Views
No views yet
revliter/internvideo_next_large_p14_res224_f1616 frames224x224[512]happy8825/internvideo_tuned1pip install decord transformers huggingface_hub
2python inference_example.py --repo_id happy8825/internvideo_tuned --video /path/to/video.mp4 --device cudanormal or abnormal.1import json, os, numpy as np, torch
2from huggingface_hub import snapshot_download
3from transformers import VideoMAEImageProcessor, AutoModel
4from decord import VideoReader
5
6ID2LABEL = {0: "normal", 1: "abnormal"}
7
8class ClassificationHead(torch.nn.Module):
9 def __init__(self, in_dim, hidden_dims, num_labels=2, dropout=0.1):
10 super().__init__()
11 dims = [in_dim] + list(hidden_dims)
12 layers = []
13 for i in range(len(dims) - 1):
14 layers += [torch.nn.Linear(dims[i], dims[i+1]), torch.nn.GELU(), torch.nn.Dropout(dropout)]
15 layers.append(torch.nn.Linear(dims[-1], num_labels))
16 self.net = torch.nn.Sequential(*layers)
17 def forward(self, x): return self.net(x)
18
19def pool_tokens(feats, expected=None):
20 if feats.dim() != 3: return feats
21 _, d1, d2 = feats.shape
22 if expected:
23 if d1 == expected: return feats.mean(dim=2)
24 if d2 == expected: return feats.mean(dim=1)
25 return feats.mean(dim=2 if d1 <= d2 else 1)
26
27repo = "happy8825/internvideo_tuned"
28local = snapshot_download(repo)
29cfg = json.load(open(os.path.join(local, "train_config.json")))
30base = cfg.get("base_model", "revliter/internvideo_next_large_p14_res224_f16")
31clip_len = int(cfg.get("clip_len", 16))
32hidden = cfg.get("hidden", [512])
33feat_dim = cfg.get("feature_dim") or cfg.get("hidden_size")
34
35processor = VideoMAEImageProcessor.from_pretrained(base)
36backbone = AutoModel.from_pretrained(base, trust_remote_code=True).eval().to("cuda")
37head = ClassificationHead(in_dim=feat_dim or backbone.config.hidden_size, hidden_dims=hidden)
38state = torch.load(os.path.join(local, "best_head.pt"), map_location="cpu")
39head.load_state_dict(state["head"]); head.eval().to("cuda")
40
41vr = VideoReader("/path/to/video.mp4")
42idxs = np.linspace(0, len(vr)-1, num=clip_len, dtype=int)
43frames = [vr[i].asnumpy() for i in idxs]
44px = processor(frames, return_tensors="pt")["pixel_values"].permute(0,2,1,3,4).to("cuda")
45with torch.no_grad(), torch.amp.autocast("cuda", dtype=torch.bfloat16):
46 feats = backbone.extract_features(pixel_values=px)
47pooled = pool_tokens(feats, expected=feat_dim)
48pred = int(head(pooled.float()).argmax(dim=-1).item())
49print(ID2LABEL.get(pred, pred))best_head.pt: classifier head weightstrain_config.json: training config (contains base model, clip_len, frame_size, hidden dims, etc.)inference_example.py: minimal inference helper