Download backend/references.py from Wizzas/DefectRAG: direct link, hf CLI and curl.
- Browser
- Download file 2.2 kB
-
https://huggingface.co/Wizzas/DefectRAG/resolve/ecb2a3f4a67e1aec2e445efe43c0f241176875ff/backend/references.py
- Command line
-
hf download hf://Wizzas/DefectRAG@ecb2a3f4a67e1aec2e445efe43c0f241176875ff/backend/references.py
-
curl -L -o references.py https://huggingface.co/Wizzas/DefectRAG/resolve/ecb2a3f4a67e1aec2e445efe43c0f241176875ff/backend/references.py
2.2 kB
| """Nearest normal reference photos: an explanation aid, never part of the verdict. | |
| Each product has a small set of diverse normal training photos, each summarised by one descriptor (the unit-length mean | |
| of its foreground patch features from the same encoder as the detector). For a new photo the same descriptor is | |
| compared with them and the closest few are named in the result, so a person can look at a similar normal photo next | |
| to the flagged one. The detector's score, threshold and verdict do not depend on any of this. | |
| """ | |
| from pathlib import Path | |
| import numpy as np | |
| import torch | |
| import torch.nn.functional as F | |
| def descriptor(tokens, mask): | |
| """Unit-length mean of the foreground patch features (all patches if the mask is empty).""" | |
| features = F.normalize(tokens.float(), dim=-1) | |
| keep = np.asarray(mask, dtype=bool) | |
| if keep.any(): | |
| features = features[torch.as_tensor(keep, device=features.device)] | |
| return F.normalize(features.mean(0), dim=0) | |
| class ReferenceIndex: | |
| """Loads references/<product>.npz lazily; a missing file simply means there are no references to show.""" | |
| def __init__(self, directory): | |
| self.directory = Path(directory) | |
| self.loaded = {} | |
| def load(self, category, device): | |
| key = (category, str(device)) | |
| if key not in self.loaded: | |
| path = self.directory / f"{category}.npz" | |
| if not path.exists(): | |
| self.loaded[key] = None | |
| else: | |
| with np.load(path, allow_pickle=False) as saved: | |
| ids = [str(i) for i in saved["ids"]] | |
| vectors = torch.from_numpy(saved["descriptors"].astype("float32")) | |
| self.loaded[key] = (ids, F.normalize(vectors, dim=-1).to(device)) | |
| return self.loaded[key] | |
| def nearest(self, category, tokens, mask, k=3): | |
| found = self.load(category, tokens.device) | |
| if found is None: | |
| return [] | |
| ids, vectors = found | |
| similarity = vectors @ descriptor(tokens, mask) | |
| top = similarity.topk(min(k, len(ids))) | |
| return [{"id": ids[int(i)], "similarity": round(float(s), 4)} for s, i in zip(top.values, top.indices)] | |