NMA: Dendrogram-based model analysis, white-box testing, and adversarial detection
Project description
NMA – Near Misses Analysis
NMA (Near Misses Analysis) is a Python package for analyzing machine learning models through dendrogram-based hierarchical clustering, white-box testing, and adversarial attack detection.
It provides visualization, explanation, and diagnostic tools to help developers and researchers understand their models’ decision boundaries, identify vulnerabilities, and detect adversarial inputs.
✨ Features
-
📊 Dendrogram construction & visualization
- Build hierarchical trees from model predictions.
- Plot full dendrograms or sub-dendrograms for specific labels.
-
🧪 White-box testing
- Identify problematic training samples likely to cause misclassification.
- Run structured analysis across source/target label pairs.
-
🛡 Adversarial attack detection
- Train a logistic regression adversarial detector.
- Detect adversarial images and compute adversarial scores.
-
🔎 Model querying & explanations
- Query images for predictions with hierarchical context.
- Generate verbal explanations of model predictions.
-
🧩 Cluster analysis tools
- Find lowest common ancestors (LCA) in the dendrogram.
- Rename clusters for more meaningful interpretation.
📦 Installation
pip install BETTER_NMA
🚀 Quickstart
from BETTER_NMA import NMA
import numpy as np
# Example data (replace with your dataset/model)
x_train = np.random.rand(100, 32, 32, 3)
y_train = np.random.randint(0, 2, size=100)
labels = ["cat", "dog"]
# Your pre-trained model (e.g., Keras, PyTorch wrapper with predict)
model = my_model
# Initialize NMA
nma = NMA(
x_train=x_train,
y_train=y_train,
labels=labels,
model=model,
explanation_method="similarity",
save_connections=True
)
# Plot dendrogram
nma.plot(title="Model Decision Hierarchy")
# Run white-box testing
issues = nma.white_box_testing(["cat"], ["dog"], analyze_results=True)
# Train adversarial detector
nma.train_adversarial_detector(authentic_images, adversarial_images)
# Detect if a new image is adversarial
result = nma.detect_attack(test_image)
# Get verbal explanation of an image
explanation = nma.verbal_explanation(test_image)
print(explanation)
📚 API Overview
Dendrogram & Visualization
plot(sub_labels=None, ...)– plot full or partial dendrogram.plot_sub_dendrogram(sub_labels, ...)– zoom into specific classes.
White-box Testing
white_box_testing(source_labels, target_labels, ...)– find problematic images.get_white_box_analysis(source_labels, target_labels, ...)– detailed analysis.
Adversarial Detection
train_adversarial_detector(authentic_images, attacked_images)– train detector.detect_attack(image, plot_result=False)– detect adversarial samples.adversarial_score(image, top_k=5)– compute adversarial score.
Query & Explanation
query_image(image, top_k=5)– get predictions & explanation.verbal_explanation(image)– generate natural language explanation.
Cluster Analysis
find_lca(label1, label2)– lowest common ancestor.change_cluster_name(cluster_id, new_name)– rename clusters.
🛠 Requirements
- Python ≥ 3.8
- NumPy, Pandas, Matplotlib, Scikit-learn
- (Optional) PyTorch / TensorFlow for model support
📖 Use Cases
- Research – interpret model predictions via hierarchical clustering.
- Robustness testing – identify adversarial vulnerabilities.
- Explainability – provide visual + verbal explanations.
- Debugging – detect mislabeled or problematic training samples.
📜 License
MIT License – free to use and modify.
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 better_nma-1.1.1.tar.gz.
File metadata
- Download URL: better_nma-1.1.1.tar.gz
- Upload date:
- Size: 31.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cae2b3528237722262afd69b2637a52780ebb139319de1c4630c5af654a8720b
|
|
| MD5 |
4f897ad61a04b9ecfca6c16258005a1c
|
|
| BLAKE2b-256 |
5ffb78277f681d945a41e7267fa7b50b0dcf635c5739a9757abd4dd0beb6bf89
|
File details
Details for the file BETTER_NMA-1.1.1-py3-none-any.whl.
File metadata
- Download URL: BETTER_NMA-1.1.1-py3-none-any.whl
- Upload date:
- Size: 37.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a02dff4cb2f04cdf56aa80c83f2d33650f6a3592a27d5620ef5ffde95d527fa
|
|
| MD5 |
b9d584d640bc0ff131143e27c97a123d
|
|
| BLAKE2b-256 |
e67cd76aab5cb737626dbe5072cc861ab3578b1068f2f7591ddd45d681fec2a7
|