fix: load hash-pinned DINOv2 weights without network

This commit is contained in:
kiyo9w 2026-09-28 13:41:27 +09:00
parent f0e2baaf3d
commit 0e85c399bb
3 changed files with 111 additions and 8 deletions

View file

@ -21,7 +21,13 @@ if os.path.exists(os.path.join(project_root, "stock")):
if current_dir not in sys.path:
sys.path.insert(0, current_dir)
from gallery import GalleryEntry, ProductGallery # noqa: E402
from gallery import ( # noqa: E402
DINOV2_SHA256,
GalleryEntry,
ProductGallery,
dinov2_weights_path,
load_dinov2,
)
def a_gallery(views, threshold=0.55):
@ -108,5 +114,35 @@ class EnrollmentTests(unittest.TestCase):
self.assertEqual(len(gallery._views_for("sku_ab")), 1)
class OfflineWeightsTests(unittest.TestCase):
def test_a_missing_weights_file_names_the_path_and_does_not_fetch(self):
previous = os.environ.get("DORAYAKI_DINOV2_WEIGHTS")
os.environ["DORAYAKI_DINOV2_WEIGHTS"] = os.path.join(
"does-not-exist", "dinov2_vits14_lvd142m.safetensors"
)
try:
with self.assertRaises(FileNotFoundError) as raised:
load_dinov2(224)
finally:
if previous is None:
os.environ.pop("DORAYAKI_DINOV2_WEIGHTS", None)
else:
os.environ["DORAYAKI_DINOV2_WEIGHTS"] = previous
message = str(raised.exception)
self.assertIn("does-not-exist", message)
self.assertIn(DINOV2_SHA256, message)
self.assertIn("does not download", message)
def test_the_default_path_stays_inside_the_repo(self):
previous = os.environ.pop("DORAYAKI_DINOV2_WEIGHTS", None)
try:
self.assertTrue(dinov2_weights_path().endswith(
os.path.join("weights", "dinov2_vits14_lvd142m.safetensors")
))
finally:
if previous is not None:
os.environ["DORAYAKI_DINOV2_WEIGHTS"] = previous
if __name__ == "__main__":
unittest.main()