|
| 1 | +""" |
| 2 | +Deepchecks quality check for the beans ViT model. |
| 3 | +""" |
| 4 | + |
| 5 | +import sys |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +from datasets import load_dataset |
| 9 | +from deepchecks.core import CheckFailure |
| 10 | +from deepchecks.vision import VisionData |
| 11 | +from deepchecks.vision.suites import train_test_validation |
| 12 | +from deepchecks.vision.vision_data import BatchOutputFormat |
| 13 | +from torch.utils.data import DataLoader |
| 14 | +from torch.utils.data import Dataset as TorchDataset |
| 15 | +from transformers import pipeline |
| 16 | + |
| 17 | +MODEL_NAME = "nateraw/vit-base-beans" |
| 18 | + |
| 19 | + |
| 20 | +class BeansDataset(TorchDataset): |
| 21 | + def __init__(self, hf_dataset): |
| 22 | + self.dataset = hf_dataset |
| 23 | + |
| 24 | + def __len__(self): |
| 25 | + return len(self.dataset) |
| 26 | + |
| 27 | + def __getitem__(self, idx): |
| 28 | + return self.dataset[idx] |
| 29 | + |
| 30 | + |
| 31 | +def make_collate_fn(pipe, label_names): |
| 32 | + def collate_fn(examples): |
| 33 | + images_pil = [ex["image"].convert("RGB") for ex in examples] |
| 34 | + labels = [ex["labels"] for ex in examples] |
| 35 | + predictions_raw = pipe(images_pil) |
| 36 | + proba = [] |
| 37 | + for preds in predictions_raw: |
| 38 | + scores = {p["label"]: p["score"] for p in preds} |
| 39 | + proba.append([scores.get(lbl, 0.0) for lbl in label_names]) |
| 40 | + images_np = [np.array(img) for img in images_pil] |
| 41 | + return BatchOutputFormat(images=images_np, labels=labels, predictions=proba) |
| 42 | + return collate_fn |
| 43 | + |
| 44 | + |
| 45 | +def build_vision_data(hf_split, pipe, label_names): |
| 46 | + dataset = BeansDataset(load_dataset("beans", split=hf_split)) |
| 47 | + label_map = {i: lbl for i, lbl in enumerate(label_names)} |
| 48 | + loader = DataLoader( |
| 49 | + dataset, |
| 50 | + batch_size=8, |
| 51 | + collate_fn=make_collate_fn(pipe, label_names), |
| 52 | + ) |
| 53 | + return VisionData(batch_loader=loader, task_type="classification", label_map=label_map) |
| 54 | + |
| 55 | + |
| 56 | +def main(): |
| 57 | + print(f"Loading model: {MODEL_NAME}...") |
| 58 | + pipe = pipeline("image-classification", model=MODEL_NAME) |
| 59 | + |
| 60 | + dataset = load_dataset("beans", split="validation") |
| 61 | + label_names = dataset.features["labels"].names |
| 62 | + |
| 63 | + print("Building VisionData...") |
| 64 | + train_data = build_vision_data("train", pipe, label_names) |
| 65 | + test_data = build_vision_data("test", pipe, label_names) |
| 66 | + |
| 67 | + print("Running Deepchecks suite...") |
| 68 | + suite = train_test_validation() |
| 69 | + result = suite.run(train_data, test_data, max_samples=500) |
| 70 | + |
| 71 | + print(f"{len(result.results)} checks executed.") |
| 72 | + |
| 73 | + # As an example, the model quality check will fail if label drift score exceeds 0.5. |
| 74 | + for check_result in result.results: |
| 75 | + if isinstance(check_result, CheckFailure): |
| 76 | + continue |
| 77 | + if check_result.check.name() == "Label Drift": |
| 78 | + drift_score = check_result.value["Samples Per Class"]["Drift score"] |
| 79 | + if drift_score > 0.5: |
| 80 | + print(f" [FAIL] Label Drift: score {drift_score:.3f} exceeds threshold 0.5") |
| 81 | + sys.exit(1) |
| 82 | + else: |
| 83 | + print(f" [PASS] Label Drift: score {drift_score:.3f} ≤ 0.5") |
| 84 | + break |
| 85 | + |
| 86 | + print("\nAll checks passed.") |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + main() |
0 commit comments