File size: 12,590 Bytes
d8f3812 6f67d1c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | ---
license: apache-2.0
---
# VisA Anomaly Classifier: Complete Runnable Setup
A Mixture of Experts (MoE) image classification system that identifies industrial product categories and detects anomalies using a product classification router and 12 specialized ResNet-18 experts.
**Hugging Face Model:** https://huggingface.co/bipulsardar/Anomaly_Classifier
## 1. Project Structure
Create the following project structure:
```text
visa-anomaly-classifier/
βββ app.py
βββ requirements.txt
βββ README.md
```
Model weights are downloaded automatically from Hugging Face when the application starts.
## 2. Requirements
- Python 3.10 or later
- Internet access for downloading model weights
- CPU or CUDA-compatible GPU
- Approximately 600 MB for model weights, plus additional runtime memory
## 3. Install Dependencies
Create a virtual environment and install the dependencies.
### Windows
```powershell
python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txt
```
### Linux / macOS / Google Colab
```bash
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```
Create `requirements.txt`:
```text
torch
torchvision
huggingface_hub
pillow
numpy
pandas
scikit-learn
tqdm
```
For GPU acceleration, install the PyTorch build compatible with your CUDA environment using the official selector: https://pytorch.org/get-started/locally/
## 4. Complete Application
Create `app.py` with the following code.
```python
import argparse
import json
import time
from pathlib import Path
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from PIL import Image
from torchvision import models, transforms
from huggingface_hub import snapshot_download
from sklearn.metrics import (
accuracy_score,
precision_score,
recall_score,
f1_score,
roc_auc_score,
classification_report,
confusion_matrix
)
CATEGORIES = [
"candle", "capsules", "cashew", "chewinggum",
"fryum", "macaroni1", "macaroni2", "pcb1",
"pcb2", "pcb3", "pcb4", "pipe_fryum"
]
IMAGE_EXTENSIONS = {
".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff"
}
REPOSITORY = "bipulsardar/Anomaly_Classifier"
IMAGE_SIZE = 224
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
TRANSFORM = transforms.Compose([
transforms.Resize((IMAGE_SIZE, IMAGE_SIZE)),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
])
def create_model(num_classes):
model = models.resnet18(weights=None)
model.fc = nn.Linear(model.fc.in_features, num_classes)
return model
def load_model(path, num_classes):
checkpoint = torch.load(
path,
map_location=DEVICE,
weights_only=True
)
state_dict = checkpoint.get("model_state_dict", checkpoint)
model = create_model(num_classes)
model.load_state_dict(state_dict)
model.to(DEVICE)
model.eval()
return model
def load_models(model_dir=None, token=None):
if model_dir:
directory = Path(model_dir)
else:
directory = Path(snapshot_download(
repo_id=REPOSITORY,
repo_type="model",
token=token
))
router = load_model(directory / "router.pth", len(CATEGORIES))
experts = {
category: load_model(
directory / f"expert_{category}.pth",
2
)
for category in CATEGORIES
}
return router, experts
def predict_image(image_path, router, experts):
with Image.open(image_path) as image:
tensor = TRANSFORM(image.convert("RGB")).unsqueeze(0).to(DEVICE)
if DEVICE.type == "cuda":
torch.cuda.synchronize()
start = time.perf_counter()
with torch.inference_mode():
router_probabilities = torch.softmax(router(tensor), dim=1)
category_id = int(router_probabilities.argmax(dim=1).item())
category = CATEGORIES[category_id]
expert_probabilities = torch.softmax(
experts[category](tensor),
dim=1
)
anomaly_score = float(expert_probabilities[0, 1].item())
prediction = int(expert_probabilities.argmax(dim=1).item())
if DEVICE.type == "cuda":
torch.cuda.synchronize()
elapsed_ms = (time.perf_counter() - start) * 1000
return {
"image_path": str(image_path),
"category": category,
"category_confidence": float(
router_probabilities[0, category_id].item()
),
"prediction": "anomaly" if prediction == 1 else "normal",
"anomaly_score": anomaly_score,
"latency_ms": elapsed_ms
}
def discover_dataset(dataset_dir):
root = Path(dataset_dir)
records = []
for category in CATEGORIES:
image_root = root / category / "Data" / "Images"
for folder, label in [("Normal", 0), ("Anomaly", 1)]:
directory = image_root / folder
if not directory.exists():
continue
for path in directory.rglob("*"):
if path.is_file() and path.suffix.lower() in IMAGE_EXTENSIONS:
records.append({
"image_path": str(path),
"true_category": category,
"true_label": label
})
dataframe = pd.DataFrame(records)
if dataframe.empty:
raise RuntimeError(
f"No VisA images found under {root}. "
"Expected category/Data/Images/Normal and Anomaly folders."
)
return dataframe.drop_duplicates("image_path").reset_index(drop=True)
def benchmark(dataset_dir, router, experts, output_dir):
output = Path(output_dir)
output.mkdir(parents=True, exist_ok=True)
dataframe = discover_dataset(dataset_dir)
results = []
for row in dataframe.itertuples(index=False):
try:
result = predict_image(row.image_path, router, experts)
result["true_category"] = row.true_category
result["true_label"] = int(row.true_label)
result["category_correct"] = (
result["category"] == row.true_category
)
results.append(result)
except Exception as error:
print(f"Failed: {row.image_path}: {error}")
result_df = pd.DataFrame(results)
if result_df.empty:
raise RuntimeError("No images were successfully evaluated.")
y_true = result_df["true_label"].to_numpy()
y_pred = (
result_df["prediction"].eq("anomaly").astype(int).to_numpy()
)
y_score = result_df["anomaly_score"].to_numpy()
try:
auc = float(roc_auc_score(y_true, y_score))
except ValueError:
auc = None
summary = {
"repository": REPOSITORY,
"device": str(DEVICE),
"images_evaluated": len(result_df),
"images_failed": len(dataframe) - len(result_df),
"router_accuracy": float(
result_df["category_correct"].mean()
),
"anomaly_accuracy": float(accuracy_score(y_true, y_pred)),
"anomaly_precision": float(
precision_score(y_true, y_pred, zero_division=0)
),
"anomaly_recall": float(
recall_score(y_true, y_pred, zero_division=0)
),
"anomaly_f1": float(
f1_score(y_true, y_pred, zero_division=0)
),
"anomaly_roc_auc": auc,
"mean_latency_ms": float(result_df["latency_ms"].mean()),
"median_latency_ms": float(result_df["latency_ms"].median()),
"p95_latency_ms": float(result_df["latency_ms"].quantile(0.95))
}
result_df.to_csv(output / "predictions.csv", index=False)
with open(output / "benchmark_summary.json", "w") as file:
json.dump(summary, file, indent=2)
print("\nBenchmark summary")
print(json.dumps(summary, indent=2))
print("\nClassification report")
print(classification_report(
y_true,
y_pred,
labels=[0, 1],
target_names=["Normal", "Anomaly"],
zero_division=0
))
print("\nConfusion matrix")
print(confusion_matrix(y_true, y_pred, labels=[0, 1]))
return result_df, summary
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--image", type=str)
parser.add_argument("--image-dir", type=str)
parser.add_argument("--dataset-dir", type=str)
parser.add_argument("--output-dir", default="benchmark_results")
parser.add_argument("--model-dir", type=str)
parser.add_argument("--hf-token", type=str)
args = parser.parse_args()
router, experts = load_models(args.model_dir, args.hf_token)
print(f"Device: {DEVICE}")
print(f"Loaded {len(experts)} experts.")
if args.image:
print(json.dumps(
predict_image(args.image, router, experts),
indent=2
))
return
if args.dataset_dir:
benchmark(
args.dataset_dir,
router,
experts,
args.output_dir
)
return
if args.image_dir:
image_paths = [
path for path in Path(args.image_dir).rglob("*")
if path.is_file() and path.suffix.lower() in IMAGE_EXTENSIONS
]
output = []
for path in image_paths:
try:
output.append(predict_image(path, router, experts))
except Exception as error:
output.append({
"image_path": str(path),
"error": str(error)
})
Path(args.output_dir).mkdir(parents=True, exist_ok=True)
pd.DataFrame(output).to_csv(
Path(args.output_dir) / "predictions.csv",
index=False
)
print(f"Processed {len(output)} images.")
return
parser.error(
"Specify --image, --image-dir, or --dataset-dir."
)
if __name__ == "__main__":
main()
```
## 5. Run Predictions
### Single image
```bash
python app.py --image ./product.jpg
```
### Batch image directory
```bash
python app.py --image-dir ./product_images --output-dir ./results
```
The predictions will be saved to `results/predictions.csv`.
### Evaluate the VisA dataset
If the dataset is extracted to `/content/VisA` in Google Colab:
```bash
python app.py --dataset-dir /content/VisA --output-dir /content/benchmark_results
```
On Windows:
```powershell
python app.py --dataset-dir "E:\Datasets\VisA" --output-dir ".\benchmark_results"
```
The benchmark outputs include per-image predictions and aggregate metrics.
## 6. Use in Google Colab
Run these cells in order.
```python
!pip -q install torch torchvision huggingface_hub pillow numpy pandas scikit-learn tqdm
```
Upload `app.py` to Colab or create it using a notebook cell, then run:
```python
!python app.py --image "/content/product.jpg"
```
Benchmark all VisA images:
```python
!python app.py --dataset-dir "/content/VisA" --output-dir "/content/benchmark_results"
```
## 7. Private Hugging Face Repositories
If the repository is private, authenticate before running the application.
```python
from huggingface_hub import login
from kaggle_secrets import UserSecretsClient
secrets = UserSecretsClient()
token = secrets.get_secret("HF_091")
login(token=token)
```
Alternatively, set `HF_TOKEN` in the environment and pass it to `load_models()`. Do not commit access tokens to source control.
## 8. Interpreting Results
The application returns:
- `category`: predicted product category.
- `category_confidence`: router softmax score for the selected category.
- `prediction`: normal or anomaly.
- `anomaly_score`: expert softmax score for the anomaly class.
- `latency_ms`: inference time for the router and selected expert.
These scores are not necessarily calibrated probabilities. Set production thresholds using a separate validation dataset.
## 9. Important Evaluation Notes
The previously reported full-dataset results are preliminary because the evaluation included training images. For a reliable estimate of generalization, evaluate on held-out test images that were never used to train or select the models.
This system performs image-level anomaly classification. It does not currently produce pixel-level defect localization.
## 10. References
- Model repository: https://huggingface.co/bipulsardar/Anomaly_Classifier
- VisA dataset: https://github.com/amazon-science/spot-diff
- PyTorch: https://pytorch.org/
- Torchvision: https://pytorch.org/vision/
- Hugging Face Hub: https://huggingface.co/docs/huggingface_hub
|