BETTER-NMA 1.0.0__tar.gz → 1.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- better_nma-1.0.1/BETTER_NMA.egg-info/PKG-INFO +174 -0
- better_nma-1.0.1/BETTER_NMA.egg-info/requires.txt +9 -0
- better_nma-1.0.1/PKG-INFO +174 -0
- better_nma-1.0.1/README.md +144 -0
- better_nma-1.0.1/setup.py +46 -0
- better_nma-1.0.0/BETTER_NMA.egg-info/PKG-INFO +0 -11
- better_nma-1.0.0/BETTER_NMA.egg-info/requires.txt +0 -8
- better_nma-1.0.0/PKG-INFO +0 -11
- better_nma-1.0.0/README.md +0 -18
- better_nma-1.0.0/setup.py +0 -19
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/__init__.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/adversarial_score.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/change_cluster_name.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/detect_attack.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/find_lca.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/main.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/nma_creator.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/plot.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/query_image.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/train_adversarial_detector.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/__init__.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/classes/__init__.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/classes/adversarial_dataset.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/classes/adversarial_detector.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/classes/dendrogram.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/classes/edges_dataframe.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/classes/preprocessing/__init__.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/classes/preprocessing/batch_predictor.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/classes/preprocessing/graph_builder.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/classes/preprocessing/heap_processor.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/classes/preprocessing/hierarchical_clustering_builder.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/classes/preprocessing/tree_node.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/classes/preprocessing/z_builder.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/classes/score_calculator.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/classes/whitebox_testing.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/enums/__init__.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/enums/explanation_method.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/enums/heap_types.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/models_utils.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/photos_uitls.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/photos_utils.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/verbal_explanation.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/wordnet_utils.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/white_box_testing.py +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA.egg-info/SOURCES.txt +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA.egg-info/dependency_links.txt +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA.egg-info/top_level.txt +0 -0
- {better_nma-1.0.0 → better_nma-1.0.1}/setup.cfg +0 -0
@@ -0,0 +1,174 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: BETTER_NMA
|
3
|
+
Version: 1.0.1
|
4
|
+
Summary: NMA: Dendrogram-based model analysis, white-box testing, and adversarial detection
|
5
|
+
Author: BETTER_XAI
|
6
|
+
Author-email: BETTERXAI2025@gmail.com
|
7
|
+
Classifier: Development Status :: 4 - Beta
|
8
|
+
Classifier: Intended Audience :: Developers
|
9
|
+
Classifier: Intended Audience :: Science/Research
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
11
|
+
Classifier: Operating System :: OS Independent
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
13
|
+
Classifier: Programming Language :: Python :: 3.8
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
19
|
+
Requires-Python: >=3.8
|
20
|
+
Description-Content-Type: text/markdown
|
21
|
+
Requires-Dist: tensorflow>=2.10.0
|
22
|
+
Requires-Dist: pandas>=1.3.0
|
23
|
+
Requires-Dist: python-igraph>=0.10.0
|
24
|
+
Requires-Dist: numpy>=1.21.0
|
25
|
+
Requires-Dist: scikit-learn>=1.0.0
|
26
|
+
Requires-Dist: matplotlib>=3.5.0
|
27
|
+
Requires-Dist: nltk>=3.7
|
28
|
+
Requires-Dist: keras>=2.10.0
|
29
|
+
Requires-Dist: Pillow>=8.0.0
|
30
|
+
|
31
|
+
|
32
|
+
---
|
33
|
+
|
34
|
+
# NMA – Near Misses Analysis
|
35
|
+
|
36
|
+
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**.
|
37
|
+
|
38
|
+
It provides visualization, explanation, and diagnostic tools to help developers and researchers understand their models’ decision boundaries, identify vulnerabilities, and detect adversarial inputs.
|
39
|
+
|
40
|
+
---
|
41
|
+
|
42
|
+
## ✨ Features
|
43
|
+
|
44
|
+
* 📊 **Dendrogram construction & visualization**
|
45
|
+
|
46
|
+
* Build hierarchical trees from model predictions.
|
47
|
+
* Plot full dendrograms or **sub-dendrograms** for specific labels.
|
48
|
+
|
49
|
+
* 🧪 **White-box testing**
|
50
|
+
|
51
|
+
* Identify problematic training samples likely to cause misclassification.
|
52
|
+
* Run structured analysis across source/target label pairs.
|
53
|
+
|
54
|
+
* 🛡 **Adversarial attack detection**
|
55
|
+
|
56
|
+
* Train a logistic regression adversarial detector.
|
57
|
+
* Detect adversarial images and compute adversarial scores.
|
58
|
+
|
59
|
+
* 🔎 **Model querying & explanations**
|
60
|
+
|
61
|
+
* Query images for predictions with hierarchical context.
|
62
|
+
* Generate **verbal explanations** of model predictions.
|
63
|
+
|
64
|
+
* 🧩 **Cluster analysis tools**
|
65
|
+
|
66
|
+
* Find lowest common ancestors (LCA) in the dendrogram.
|
67
|
+
* Rename clusters for more meaningful interpretation.
|
68
|
+
|
69
|
+
---
|
70
|
+
|
71
|
+
## 📦 Installation
|
72
|
+
|
73
|
+
```bash
|
74
|
+
pip install BETTER_NMA
|
75
|
+
```
|
76
|
+
|
77
|
+
---
|
78
|
+
|
79
|
+
## 🚀 Quickstart
|
80
|
+
|
81
|
+
```python
|
82
|
+
from BETTER_NMA import NMA
|
83
|
+
import numpy as np
|
84
|
+
|
85
|
+
# Example data (replace with your dataset/model)
|
86
|
+
x_train = np.random.rand(100, 32, 32, 3)
|
87
|
+
y_train = np.random.randint(0, 2, size=100)
|
88
|
+
labels = ["cat", "dog"]
|
89
|
+
|
90
|
+
# Your pre-trained model (e.g., Keras, PyTorch wrapper with predict)
|
91
|
+
model = my_model
|
92
|
+
|
93
|
+
# Initialize NMA
|
94
|
+
nma = NMA(
|
95
|
+
x_train=x_train,
|
96
|
+
y_train=y_train,
|
97
|
+
labels=labels,
|
98
|
+
model=model,
|
99
|
+
explanation_method="similarity",
|
100
|
+
save_connections=True
|
101
|
+
)
|
102
|
+
|
103
|
+
# Plot dendrogram
|
104
|
+
nma.plot(title="Model Decision Hierarchy")
|
105
|
+
|
106
|
+
# Run white-box testing
|
107
|
+
issues = nma.white_box_testing(["cat"], ["dog"], analyze_results=True)
|
108
|
+
|
109
|
+
# Train adversarial detector
|
110
|
+
nma.train_adversarial_detector(authentic_images, adversarial_images)
|
111
|
+
|
112
|
+
# Detect if a new image is adversarial
|
113
|
+
result = nma.detect_attack(test_image)
|
114
|
+
|
115
|
+
# Get verbal explanation of an image
|
116
|
+
explanation = nma.verbal_explanation(test_image)
|
117
|
+
print(explanation)
|
118
|
+
```
|
119
|
+
|
120
|
+
---
|
121
|
+
|
122
|
+
## 📚 API Overview
|
123
|
+
|
124
|
+
### Dendrogram & Visualization
|
125
|
+
|
126
|
+
* `plot(sub_labels=None, ...)` – plot full or partial dendrogram.
|
127
|
+
* `plot_sub_dendrogram(sub_labels, ...)` – zoom into specific classes.
|
128
|
+
|
129
|
+
### White-box Testing
|
130
|
+
|
131
|
+
* `white_box_testing(source_labels, target_labels, ...)` – find problematic images.
|
132
|
+
* `get_white_box_analysis(source_labels, target_labels, ...)` – detailed analysis.
|
133
|
+
|
134
|
+
### Adversarial Detection
|
135
|
+
|
136
|
+
* `train_adversarial_detector(authentic_images, attacked_images)` – train detector.
|
137
|
+
* `detect_attack(image, plot_result=False)` – detect adversarial samples.
|
138
|
+
* `adversarial_score(image, top_k=5)` – compute adversarial score.
|
139
|
+
|
140
|
+
### Query & Explanation
|
141
|
+
|
142
|
+
* `query_image(image, top_k=5)` – get predictions & explanation.
|
143
|
+
* `verbal_explanation(image)` – generate natural language explanation.
|
144
|
+
|
145
|
+
### Cluster Analysis
|
146
|
+
|
147
|
+
* `find_lca(label1, label2)` – lowest common ancestor.
|
148
|
+
* `change_cluster_name(cluster_id, new_name)` – rename clusters.
|
149
|
+
|
150
|
+
---
|
151
|
+
|
152
|
+
## 🛠 Requirements
|
153
|
+
|
154
|
+
* Python ≥ 3.8
|
155
|
+
* NumPy, Pandas, Matplotlib, Scikit-learn
|
156
|
+
* (Optional) PyTorch / TensorFlow for model support
|
157
|
+
|
158
|
+
---
|
159
|
+
|
160
|
+
## 📖 Use Cases
|
161
|
+
|
162
|
+
* **Research** – interpret model predictions via hierarchical clustering.
|
163
|
+
* **Robustness testing** – identify adversarial vulnerabilities.
|
164
|
+
* **Explainability** – provide visual + verbal explanations.
|
165
|
+
* **Debugging** – detect mislabeled or problematic training samples.
|
166
|
+
|
167
|
+
---
|
168
|
+
|
169
|
+
## 📜 License
|
170
|
+
|
171
|
+
MIT License – free to use and modify.
|
172
|
+
|
173
|
+
---
|
174
|
+
|
@@ -0,0 +1,174 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: BETTER_NMA
|
3
|
+
Version: 1.0.1
|
4
|
+
Summary: NMA: Dendrogram-based model analysis, white-box testing, and adversarial detection
|
5
|
+
Author: BETTER_XAI
|
6
|
+
Author-email: BETTERXAI2025@gmail.com
|
7
|
+
Classifier: Development Status :: 4 - Beta
|
8
|
+
Classifier: Intended Audience :: Developers
|
9
|
+
Classifier: Intended Audience :: Science/Research
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
11
|
+
Classifier: Operating System :: OS Independent
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
13
|
+
Classifier: Programming Language :: Python :: 3.8
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
19
|
+
Requires-Python: >=3.8
|
20
|
+
Description-Content-Type: text/markdown
|
21
|
+
Requires-Dist: tensorflow>=2.10.0
|
22
|
+
Requires-Dist: pandas>=1.3.0
|
23
|
+
Requires-Dist: python-igraph>=0.10.0
|
24
|
+
Requires-Dist: numpy>=1.21.0
|
25
|
+
Requires-Dist: scikit-learn>=1.0.0
|
26
|
+
Requires-Dist: matplotlib>=3.5.0
|
27
|
+
Requires-Dist: nltk>=3.7
|
28
|
+
Requires-Dist: keras>=2.10.0
|
29
|
+
Requires-Dist: Pillow>=8.0.0
|
30
|
+
|
31
|
+
|
32
|
+
---
|
33
|
+
|
34
|
+
# NMA – Near Misses Analysis
|
35
|
+
|
36
|
+
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**.
|
37
|
+
|
38
|
+
It provides visualization, explanation, and diagnostic tools to help developers and researchers understand their models’ decision boundaries, identify vulnerabilities, and detect adversarial inputs.
|
39
|
+
|
40
|
+
---
|
41
|
+
|
42
|
+
## ✨ Features
|
43
|
+
|
44
|
+
* 📊 **Dendrogram construction & visualization**
|
45
|
+
|
46
|
+
* Build hierarchical trees from model predictions.
|
47
|
+
* Plot full dendrograms or **sub-dendrograms** for specific labels.
|
48
|
+
|
49
|
+
* 🧪 **White-box testing**
|
50
|
+
|
51
|
+
* Identify problematic training samples likely to cause misclassification.
|
52
|
+
* Run structured analysis across source/target label pairs.
|
53
|
+
|
54
|
+
* 🛡 **Adversarial attack detection**
|
55
|
+
|
56
|
+
* Train a logistic regression adversarial detector.
|
57
|
+
* Detect adversarial images and compute adversarial scores.
|
58
|
+
|
59
|
+
* 🔎 **Model querying & explanations**
|
60
|
+
|
61
|
+
* Query images for predictions with hierarchical context.
|
62
|
+
* Generate **verbal explanations** of model predictions.
|
63
|
+
|
64
|
+
* 🧩 **Cluster analysis tools**
|
65
|
+
|
66
|
+
* Find lowest common ancestors (LCA) in the dendrogram.
|
67
|
+
* Rename clusters for more meaningful interpretation.
|
68
|
+
|
69
|
+
---
|
70
|
+
|
71
|
+
## 📦 Installation
|
72
|
+
|
73
|
+
```bash
|
74
|
+
pip install BETTER_NMA
|
75
|
+
```
|
76
|
+
|
77
|
+
---
|
78
|
+
|
79
|
+
## 🚀 Quickstart
|
80
|
+
|
81
|
+
```python
|
82
|
+
from BETTER_NMA import NMA
|
83
|
+
import numpy as np
|
84
|
+
|
85
|
+
# Example data (replace with your dataset/model)
|
86
|
+
x_train = np.random.rand(100, 32, 32, 3)
|
87
|
+
y_train = np.random.randint(0, 2, size=100)
|
88
|
+
labels = ["cat", "dog"]
|
89
|
+
|
90
|
+
# Your pre-trained model (e.g., Keras, PyTorch wrapper with predict)
|
91
|
+
model = my_model
|
92
|
+
|
93
|
+
# Initialize NMA
|
94
|
+
nma = NMA(
|
95
|
+
x_train=x_train,
|
96
|
+
y_train=y_train,
|
97
|
+
labels=labels,
|
98
|
+
model=model,
|
99
|
+
explanation_method="similarity",
|
100
|
+
save_connections=True
|
101
|
+
)
|
102
|
+
|
103
|
+
# Plot dendrogram
|
104
|
+
nma.plot(title="Model Decision Hierarchy")
|
105
|
+
|
106
|
+
# Run white-box testing
|
107
|
+
issues = nma.white_box_testing(["cat"], ["dog"], analyze_results=True)
|
108
|
+
|
109
|
+
# Train adversarial detector
|
110
|
+
nma.train_adversarial_detector(authentic_images, adversarial_images)
|
111
|
+
|
112
|
+
# Detect if a new image is adversarial
|
113
|
+
result = nma.detect_attack(test_image)
|
114
|
+
|
115
|
+
# Get verbal explanation of an image
|
116
|
+
explanation = nma.verbal_explanation(test_image)
|
117
|
+
print(explanation)
|
118
|
+
```
|
119
|
+
|
120
|
+
---
|
121
|
+
|
122
|
+
## 📚 API Overview
|
123
|
+
|
124
|
+
### Dendrogram & Visualization
|
125
|
+
|
126
|
+
* `plot(sub_labels=None, ...)` – plot full or partial dendrogram.
|
127
|
+
* `plot_sub_dendrogram(sub_labels, ...)` – zoom into specific classes.
|
128
|
+
|
129
|
+
### White-box Testing
|
130
|
+
|
131
|
+
* `white_box_testing(source_labels, target_labels, ...)` – find problematic images.
|
132
|
+
* `get_white_box_analysis(source_labels, target_labels, ...)` – detailed analysis.
|
133
|
+
|
134
|
+
### Adversarial Detection
|
135
|
+
|
136
|
+
* `train_adversarial_detector(authentic_images, attacked_images)` – train detector.
|
137
|
+
* `detect_attack(image, plot_result=False)` – detect adversarial samples.
|
138
|
+
* `adversarial_score(image, top_k=5)` – compute adversarial score.
|
139
|
+
|
140
|
+
### Query & Explanation
|
141
|
+
|
142
|
+
* `query_image(image, top_k=5)` – get predictions & explanation.
|
143
|
+
* `verbal_explanation(image)` – generate natural language explanation.
|
144
|
+
|
145
|
+
### Cluster Analysis
|
146
|
+
|
147
|
+
* `find_lca(label1, label2)` – lowest common ancestor.
|
148
|
+
* `change_cluster_name(cluster_id, new_name)` – rename clusters.
|
149
|
+
|
150
|
+
---
|
151
|
+
|
152
|
+
## 🛠 Requirements
|
153
|
+
|
154
|
+
* Python ≥ 3.8
|
155
|
+
* NumPy, Pandas, Matplotlib, Scikit-learn
|
156
|
+
* (Optional) PyTorch / TensorFlow for model support
|
157
|
+
|
158
|
+
---
|
159
|
+
|
160
|
+
## 📖 Use Cases
|
161
|
+
|
162
|
+
* **Research** – interpret model predictions via hierarchical clustering.
|
163
|
+
* **Robustness testing** – identify adversarial vulnerabilities.
|
164
|
+
* **Explainability** – provide visual + verbal explanations.
|
165
|
+
* **Debugging** – detect mislabeled or problematic training samples.
|
166
|
+
|
167
|
+
---
|
168
|
+
|
169
|
+
## 📜 License
|
170
|
+
|
171
|
+
MIT License – free to use and modify.
|
172
|
+
|
173
|
+
---
|
174
|
+
|
@@ -0,0 +1,144 @@
|
|
1
|
+
|
2
|
+
---
|
3
|
+
|
4
|
+
# NMA – Near Misses Analysis
|
5
|
+
|
6
|
+
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**.
|
7
|
+
|
8
|
+
It provides visualization, explanation, and diagnostic tools to help developers and researchers understand their models’ decision boundaries, identify vulnerabilities, and detect adversarial inputs.
|
9
|
+
|
10
|
+
---
|
11
|
+
|
12
|
+
## ✨ Features
|
13
|
+
|
14
|
+
* 📊 **Dendrogram construction & visualization**
|
15
|
+
|
16
|
+
* Build hierarchical trees from model predictions.
|
17
|
+
* Plot full dendrograms or **sub-dendrograms** for specific labels.
|
18
|
+
|
19
|
+
* 🧪 **White-box testing**
|
20
|
+
|
21
|
+
* Identify problematic training samples likely to cause misclassification.
|
22
|
+
* Run structured analysis across source/target label pairs.
|
23
|
+
|
24
|
+
* 🛡 **Adversarial attack detection**
|
25
|
+
|
26
|
+
* Train a logistic regression adversarial detector.
|
27
|
+
* Detect adversarial images and compute adversarial scores.
|
28
|
+
|
29
|
+
* 🔎 **Model querying & explanations**
|
30
|
+
|
31
|
+
* Query images for predictions with hierarchical context.
|
32
|
+
* Generate **verbal explanations** of model predictions.
|
33
|
+
|
34
|
+
* 🧩 **Cluster analysis tools**
|
35
|
+
|
36
|
+
* Find lowest common ancestors (LCA) in the dendrogram.
|
37
|
+
* Rename clusters for more meaningful interpretation.
|
38
|
+
|
39
|
+
---
|
40
|
+
|
41
|
+
## 📦 Installation
|
42
|
+
|
43
|
+
```bash
|
44
|
+
pip install BETTER_NMA
|
45
|
+
```
|
46
|
+
|
47
|
+
---
|
48
|
+
|
49
|
+
## 🚀 Quickstart
|
50
|
+
|
51
|
+
```python
|
52
|
+
from BETTER_NMA import NMA
|
53
|
+
import numpy as np
|
54
|
+
|
55
|
+
# Example data (replace with your dataset/model)
|
56
|
+
x_train = np.random.rand(100, 32, 32, 3)
|
57
|
+
y_train = np.random.randint(0, 2, size=100)
|
58
|
+
labels = ["cat", "dog"]
|
59
|
+
|
60
|
+
# Your pre-trained model (e.g., Keras, PyTorch wrapper with predict)
|
61
|
+
model = my_model
|
62
|
+
|
63
|
+
# Initialize NMA
|
64
|
+
nma = NMA(
|
65
|
+
x_train=x_train,
|
66
|
+
y_train=y_train,
|
67
|
+
labels=labels,
|
68
|
+
model=model,
|
69
|
+
explanation_method="similarity",
|
70
|
+
save_connections=True
|
71
|
+
)
|
72
|
+
|
73
|
+
# Plot dendrogram
|
74
|
+
nma.plot(title="Model Decision Hierarchy")
|
75
|
+
|
76
|
+
# Run white-box testing
|
77
|
+
issues = nma.white_box_testing(["cat"], ["dog"], analyze_results=True)
|
78
|
+
|
79
|
+
# Train adversarial detector
|
80
|
+
nma.train_adversarial_detector(authentic_images, adversarial_images)
|
81
|
+
|
82
|
+
# Detect if a new image is adversarial
|
83
|
+
result = nma.detect_attack(test_image)
|
84
|
+
|
85
|
+
# Get verbal explanation of an image
|
86
|
+
explanation = nma.verbal_explanation(test_image)
|
87
|
+
print(explanation)
|
88
|
+
```
|
89
|
+
|
90
|
+
---
|
91
|
+
|
92
|
+
## 📚 API Overview
|
93
|
+
|
94
|
+
### Dendrogram & Visualization
|
95
|
+
|
96
|
+
* `plot(sub_labels=None, ...)` – plot full or partial dendrogram.
|
97
|
+
* `plot_sub_dendrogram(sub_labels, ...)` – zoom into specific classes.
|
98
|
+
|
99
|
+
### White-box Testing
|
100
|
+
|
101
|
+
* `white_box_testing(source_labels, target_labels, ...)` – find problematic images.
|
102
|
+
* `get_white_box_analysis(source_labels, target_labels, ...)` – detailed analysis.
|
103
|
+
|
104
|
+
### Adversarial Detection
|
105
|
+
|
106
|
+
* `train_adversarial_detector(authentic_images, attacked_images)` – train detector.
|
107
|
+
* `detect_attack(image, plot_result=False)` – detect adversarial samples.
|
108
|
+
* `adversarial_score(image, top_k=5)` – compute adversarial score.
|
109
|
+
|
110
|
+
### Query & Explanation
|
111
|
+
|
112
|
+
* `query_image(image, top_k=5)` – get predictions & explanation.
|
113
|
+
* `verbal_explanation(image)` – generate natural language explanation.
|
114
|
+
|
115
|
+
### Cluster Analysis
|
116
|
+
|
117
|
+
* `find_lca(label1, label2)` – lowest common ancestor.
|
118
|
+
* `change_cluster_name(cluster_id, new_name)` – rename clusters.
|
119
|
+
|
120
|
+
---
|
121
|
+
|
122
|
+
## 🛠 Requirements
|
123
|
+
|
124
|
+
* Python ≥ 3.8
|
125
|
+
* NumPy, Pandas, Matplotlib, Scikit-learn
|
126
|
+
* (Optional) PyTorch / TensorFlow for model support
|
127
|
+
|
128
|
+
---
|
129
|
+
|
130
|
+
## 📖 Use Cases
|
131
|
+
|
132
|
+
* **Research** – interpret model predictions via hierarchical clustering.
|
133
|
+
* **Robustness testing** – identify adversarial vulnerabilities.
|
134
|
+
* **Explainability** – provide visual + verbal explanations.
|
135
|
+
* **Debugging** – detect mislabeled or problematic training samples.
|
136
|
+
|
137
|
+
---
|
138
|
+
|
139
|
+
## 📜 License
|
140
|
+
|
141
|
+
MIT License – free to use and modify.
|
142
|
+
|
143
|
+
---
|
144
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
from setuptools import setup, find_packages
|
2
|
+
|
3
|
+
try:
|
4
|
+
with open("README.md", "r", encoding="utf-8") as fh:
|
5
|
+
long_description = fh.read()
|
6
|
+
except FileNotFoundError:
|
7
|
+
long_description = "Network Model Analysis for adversarial detection and explanation"
|
8
|
+
|
9
|
+
setup(
|
10
|
+
name="BETTER_NMA",
|
11
|
+
version="1.0.1",
|
12
|
+
author="BETTER_XAI",
|
13
|
+
author_email="BETTERXAI2025@gmail.com",
|
14
|
+
description="NMA: Dendrogram-based model analysis, white-box testing, and adversarial detection",
|
15
|
+
long_description=long_description,
|
16
|
+
long_description_content_type="text/markdown",
|
17
|
+
classifiers=[
|
18
|
+
"Development Status :: 4 - Beta",
|
19
|
+
"Intended Audience :: Developers",
|
20
|
+
"Intended Audience :: Science/Research",
|
21
|
+
"License :: OSI Approved :: MIT License",
|
22
|
+
"Operating System :: OS Independent",
|
23
|
+
"Programming Language :: Python :: 3",
|
24
|
+
"Programming Language :: Python :: 3.8",
|
25
|
+
"Programming Language :: Python :: 3.9",
|
26
|
+
"Programming Language :: Python :: 3.10",
|
27
|
+
"Programming Language :: Python :: 3.11",
|
28
|
+
"Programming Language :: Python :: 3.12",
|
29
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
30
|
+
],
|
31
|
+
python_requires=">=3.8",
|
32
|
+
packages=find_packages(),
|
33
|
+
install_requires=[
|
34
|
+
"tensorflow>=2.10.0",
|
35
|
+
"pandas>=1.3.0",
|
36
|
+
"python-igraph>=0.10.0",
|
37
|
+
"numpy>=1.21.0",
|
38
|
+
"scikit-learn>=1.0.0",
|
39
|
+
"matplotlib>=3.5.0",
|
40
|
+
"nltk>=3.7",
|
41
|
+
"keras>=2.10.0",
|
42
|
+
"Pillow>=8.0.0",
|
43
|
+
],
|
44
|
+
)
|
45
|
+
|
46
|
+
|
@@ -1,11 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.1
|
2
|
-
Name: BETTER_NMA
|
3
|
-
Version: 1.0.0
|
4
|
-
Requires-Dist: tensorflow
|
5
|
-
Requires-Dist: pandas
|
6
|
-
Requires-Dist: igraph
|
7
|
-
Requires-Dist: numpy
|
8
|
-
Requires-Dist: scikit-learn
|
9
|
-
Requires-Dist: matplotlib
|
10
|
-
Requires-Dist: nltk
|
11
|
-
Requires-Dist: keras
|
better_nma-1.0.0/PKG-INFO
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.1
|
2
|
-
Name: BETTER_NMA
|
3
|
-
Version: 1.0.0
|
4
|
-
Requires-Dist: tensorflow
|
5
|
-
Requires-Dist: pandas
|
6
|
-
Requires-Dist: igraph
|
7
|
-
Requires-Dist: numpy
|
8
|
-
Requires-Dist: scikit-learn
|
9
|
-
Requires-Dist: matplotlib
|
10
|
-
Requires-Dist: nltk
|
11
|
-
Requires-Dist: keras
|
better_nma-1.0.0/README.md
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
# BETTER-Package
|
2
|
-
|
3
|
-
### changes from previous code:
|
4
|
-
- no attacks types
|
5
|
-
- no datasets
|
6
|
-
- GraphType -> ExplanationMethod
|
7
|
-
- No available dataset info / train / test sets
|
8
|
-
- No local files saving - no eages df, no CSVs etc..
|
9
|
-
|
10
|
-
|
11
|
-
## Functionalities to add - untill Sunday 31/8
|
12
|
-
- API for calculating the LCA of two given classes
|
13
|
-
- API for givin the user the score of one image
|
14
|
-
- Plotting with cluster names - in two stages - return a json (1) and then plot the info from the json (2) (so that the user could modify the cluster names before plotting)
|
15
|
-
- Automatic WBT - problematic connections identification
|
16
|
-
|
17
|
-
## TODO:
|
18
|
-
- search for a package that plots with cluster names
|
better_nma-1.0.0/setup.py
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
from setuptools import setup, find_packages
|
2
|
-
|
3
|
-
setup(
|
4
|
-
name="BETTER_NMA",
|
5
|
-
version="1.0.0",
|
6
|
-
packages=find_packages(),
|
7
|
-
install_requires=[
|
8
|
-
"tensorflow",
|
9
|
-
"pandas",
|
10
|
-
"igraph",
|
11
|
-
"numpy",
|
12
|
-
"scikit-learn",
|
13
|
-
"matplotlib",
|
14
|
-
"nltk",
|
15
|
-
"keras",
|
16
|
-
],
|
17
|
-
)
|
18
|
-
|
19
|
-
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/classes/preprocessing/batch_predictor.py
RENAMED
File without changes
|
{better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/classes/preprocessing/graph_builder.py
RENAMED
File without changes
|
{better_nma-1.0.0 → better_nma-1.0.1}/BETTER_NMA/utilss/classes/preprocessing/heap_processor.py
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|