164 lines
7.1 KiB
Python
164 lines
7.1 KiB
Python
"""MIT Han Lab EfficientViT ImageNet classifiers, using the official factory.
|
|
|
|
No downloads, package installation, key remapping, or architecture fallback occur
|
|
here. The pinned official package and its dependencies live in a private cache;
|
|
the shared venv is unchanged. Missing files remain errors, not readiness evidence.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import importlib
|
|
import importlib.util
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
UPSTREAM_REPOSITORY = "https://github.com/mit-han-lab/efficientvit"
|
|
# The original classification release avoids later SAM/DC_AE eager imports.
|
|
# Its unmodified factory strictly accepts all four official r224 checkpoints.
|
|
UPSTREAM_COMMIT = "b3ed56d97ac8ff0cc6add7ad2533c42a7488d736"
|
|
RUNTIME_ROOT = Path.home() / "bench-cache/sources/efficientvit-cls"
|
|
CHECKPOINT_REPOSITORY = "han-cai/efficientvit-cls"
|
|
CHECKPOINT_REVISION = "df3d006c2567f9e322b03731f20fe4405a1ab090"
|
|
INPUT_SPEC = "1x3x224x224"
|
|
|
|
# Hashes are the HF LFS object IDs, also verified against downloaded bytes.
|
|
_CHECKPOINTS = {
|
|
"b0": (13741687, "feebc4d3ebf610673c8ed1d92b21fa2c2e57661868173a37126b734ca5dc3771"),
|
|
"b1": (36575137, "bf8798aa03ed2bba21fc8d58f43ca9a92096f3f03bd700e1df1dcde11559f499"),
|
|
"b2": (97581671, "6ccfecc65460856cb6415f2356deb8b90e0216960f661f5055aef173f8ca421d"),
|
|
"b3": (194992573, "3c5be1a11a8a41b3ac4e30e5bff8bf99065c98b4b2d586e1803fd7571f76fb0d"),
|
|
}
|
|
|
|
# Check the actual architecture, factory and loader, not just a package name.
|
|
# This pins core source identity; it is not a full environment attestation.
|
|
_SOURCE_SHA256 = {
|
|
"__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
|
"cls_model_zoo.py": "c5e21fa447add578968ce6ed179c6e877aa3414b46f1d21af45e947610030599",
|
|
"models/efficientvit/__init__.py": "6ec6831645b992d01f479174f55df2c026a05adf099a9eebb45b9690c84b8051",
|
|
"models/efficientvit/cls.py": "41534fcd771aa42d9db2cb95d7580dfdf5ac5d54b57b2df8ac2b35dd4be7414e",
|
|
"models/efficientvit/backbone.py": "f30d4460b3c2000421fc3421d1cbb3d501233e13fa4f80f9df7bbc7ed5664863",
|
|
"models/nn/ops.py": "a5c177a1d3f9d1d8c85647c57d01295506f84ba53b763b0ba77095be34e174e0",
|
|
"models/nn/norm.py": "45d34b3b5002c8d76557ed3017c02ccfc31723f2d84efd2f0cd7074706132cf5",
|
|
"models/nn/act.py": "7b71a6626dd7ebd7bf0cba283f3f3f5ec3da6ebec0d7c04b2fbafa1969abd7f8",
|
|
"models/utils/network.py": "db63b048a48c1edd47e6f401d0cc7d96dfc388b7e37f568acdef5c7a83acc5c4",
|
|
}
|
|
|
|
PREPROCESS = {
|
|
"input_spec": INPUT_SPEC,
|
|
"color": "RGB",
|
|
"crop_ratio": 0.95,
|
|
"resize_shorter": 236, # ceil(224 / 0.95)
|
|
"interpolation": "bicubic",
|
|
"center_crop": 224,
|
|
"scale": 1.0 / 255.0,
|
|
"mean": (0.485, 0.456, 0.406),
|
|
"std": (0.229, 0.224, 0.225),
|
|
"output_layout": "NCHW",
|
|
}
|
|
|
|
|
|
def checkpoint_info(variant: str) -> dict:
|
|
"""Return an immutable-revision HTTPS URI suitable for existing fetch_http."""
|
|
if variant not in _CHECKPOINTS:
|
|
raise ValueError(f"Unsupported MIT EfficientViT classification variant: {variant!r}")
|
|
size, sha256 = _CHECKPOINTS[variant]
|
|
filename = f"efficientvit_{variant}_r224.pt"
|
|
return {
|
|
"model_id": f"efficientvit.{variant}",
|
|
"factory_name": f"{variant}-r224",
|
|
"filename": filename,
|
|
"url": f"https://huggingface.co/{CHECKPOINT_REPOSITORY}/resolve/{CHECKPOINT_REVISION}/{filename}",
|
|
"size": size,
|
|
"sha256": sha256,
|
|
}
|
|
|
|
|
|
def _sha256(path: Path) -> str:
|
|
digest = hashlib.sha256()
|
|
with path.open("rb") as stream:
|
|
for chunk in iter(lambda: stream.read(1 << 20), b""):
|
|
digest.update(chunk)
|
|
return digest.hexdigest()
|
|
|
|
|
|
def verify_checkpoint(variant: str, weights: Path) -> dict:
|
|
"""Reject HTML, wrong resolutions/variants, and converted or random weights."""
|
|
info = checkpoint_info(variant)
|
|
weights = Path(weights)
|
|
if not weights.is_file():
|
|
raise FileNotFoundError(f"Official checkpoint required: {weights}; source: {info['url']}")
|
|
if weights.stat().st_size != info["size"] or _sha256(weights) != info["sha256"]:
|
|
raise ValueError(
|
|
f"Checkpoint identity mismatch for {info['model_id']}: {weights}; "
|
|
f"expected {info['size']} bytes, SHA256 {info['sha256']} from {info['url']}"
|
|
)
|
|
return info
|
|
|
|
|
|
def _check_input_spec(input_spec: str) -> None:
|
|
if input_spec != INPUT_SPEC:
|
|
raise ValueError(f"This adapter pins the official r224 recipe: expected {INPUT_SPEC}, got {input_spec!r}")
|
|
|
|
|
|
def _official_factory():
|
|
source = RUNTIME_ROOT / f"efficientvit-{UPSTREAM_COMMIT}"
|
|
root = source / "efficientvit"
|
|
for relative, expected in _SOURCE_SHA256.items():
|
|
path = root / relative
|
|
if not path.is_file() or _sha256(path) != expected:
|
|
raise RuntimeError(
|
|
f"Unverified EfficientViT source: {path}; expected core source from "
|
|
f"{UPSTREAM_REPOSITORY}/tree/{UPSTREAM_COMMIT} (SHA256 {expected})."
|
|
)
|
|
# Only this process sees the private dependencies. Restore the import path
|
|
# after upstream's imports; never change PYTHONPATH or the shared venv.
|
|
original_path = sys.path[:]
|
|
try:
|
|
sys.path[:0] = [str(source), str(RUNTIME_ROOT / "deps")]
|
|
spec = importlib.util.find_spec("efficientvit")
|
|
if spec is None or spec.origin is None or Path(spec.origin).resolve() != (root / "__init__.py").resolve():
|
|
raise RuntimeError("A different EfficientViT package is already loaded; use a fresh worker.")
|
|
module = importlib.import_module("efficientvit.cls_model_zoo")
|
|
return module.create_cls_model
|
|
except ImportError as exc:
|
|
raise RuntimeError(
|
|
f"Official EfficientViT import failed ({exc}). Prepare pinned private dependencies "
|
|
f"under {RUNTIME_ROOT / 'deps'}. No fallback is used."
|
|
) from exc
|
|
finally:
|
|
sys.path[:] = original_path
|
|
|
|
|
|
def build_variant(variant: str, weights: Path, input_spec: str):
|
|
"""Load a hash-verified checkpoint via upstream's strict state_dict loader.
|
|
|
|
Upstream keeps norm epsilon at 1e-5 for the B series and reads the nested
|
|
state_dict with torch.load(map_location='cpu'), then load_state_dict(strict=True).
|
|
"""
|
|
_check_input_spec(input_spec)
|
|
info = verify_checkpoint(variant, weights)
|
|
factory = _official_factory()
|
|
model = factory(
|
|
name=info["factory_name"], pretrained=True, weight_url=str(Path(weights).resolve())
|
|
)
|
|
return model.eval()
|
|
|
|
|
|
def preprocess(image, input_spec: str = INPUT_SPEC):
|
|
"""Official PIL/torchvision evaluation transform, returned as float32 NCHW.
|
|
|
|
Shared by baseline, calibration, G2 and quantized evaluation through the
|
|
harness adapter hook; it is not an in-model normalization wrapper.
|
|
"""
|
|
_check_input_spec(input_spec)
|
|
from torchvision import transforms
|
|
from torchvision.transforms.functional import InterpolationMode
|
|
|
|
transform = transforms.Compose([
|
|
transforms.Resize(236, interpolation=InterpolationMode.BICUBIC),
|
|
transforms.CenterCrop(224),
|
|
transforms.ToTensor(),
|
|
transforms.Normalize(mean=PREPROCESS["mean"], std=PREPROCESS["std"]),
|
|
])
|
|
return transform(image.convert("RGB")).unsqueeze(0).numpy()
|