mantis-repro-bundle / zeroshot_probe.py
ancs21's picture
Mantis reproduction bundle
f9a6764 verified
Raw
History Blame Contribute Delete
2.3 kB
"""Mantis (#12379) claim check: frozen zero-shot features are useful.
Loads small UCR datasets, extracts FROZEN Mantis-8M embeddings (no fine-tuning),
fits a linear classifier (logistic regression) on top, reports test accuracy.
If the frozen features are useful, accuracy should be well above chance and
competitive with standard TS classifiers.
"""
import warnings; warnings.filterwarnings("ignore")
import numpy as np, torch
from mantis.architecture import Mantis8M
from mantis.trainer import MantisTrainer
from aeon.datasets import load_classification
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
MANTIS_LEN = 512
net = Mantis8M(device="cpu").from_pretrained("paris-noah/Mantis-8M")
clf_wrap = MantisTrainer(device="cpu", network=net)
def resample_to(x, L=MANTIS_LEN):
# x: [n, 1, T] -> [n, 1, L] via linear interpolation
t = torch.tensor(np.asarray(x), dtype=torch.float32)
if t.ndim == 3 and t.shape[1] != 1: # take first channel if multivariate
t = t[:, :1, :]
t = torch.nn.functional.interpolate(t, size=L, mode="linear", align_corners=False)
return t.numpy()
datasets = ["ECG200", "GunPoint", "Coffee", "ItalyPowerDemand"]
print(f"{'dataset':18} {'n_tr':>5} {'n_te':>5} {'len':>5} {'chance':>7} {'frozen-acc':>11}")
accs = []
for name in datasets:
try:
Xtr, ytr = load_classification(name, split="train")
Xte, yte = load_classification(name, split="test")
except Exception as e:
print(f"{name:18} load failed: {e}"); continue
T0 = Xtr.shape[-1]
Etr = clf_wrap.transform(resample_to(Xtr))
Ete = clf_wrap.transform(resample_to(Xte))
sc = StandardScaler().fit(Etr)
lr = LogisticRegression(max_iter=2000).fit(sc.transform(Etr), ytr)
acc = lr.score(sc.transform(Ete), yte)
# majority-class baseline
vals, cnts = np.unique(ytr, return_counts=True)
chance = max(cnts) / len(ytr)
accs.append(acc)
print(f"{name:18} {len(ytr):>5} {len(yte):>5} {T0:>5} {chance:>7.3f} {acc:>11.3f}")
if accs:
print(f"\nmean frozen-feature accuracy over {len(accs)} datasets: {np.mean(accs):.3f}")
print("Claim: frozen zero-shot Mantis features are useful (well above chance) -> "
+ ("SUPPORTED" if np.mean(accs) > 0.8 else "WEAK"))