Views
No views yet


inference/inference_for_colab.ipynb and demonstrates how to run local inference using the fine-tuned VideoMAE model.pip install transformers torch decord huggingface-hub1import torch
2import torch.nn as nn
3import numpy as np
4from transformers import VideoMAEImageProcessor, VideoMAEForVideoClassification
5from decord import VideoReader, cpu
6from huggingface_hub import hf_hub_download
7
8MODEL_NAME = "star092304/vi-sign-language-videomae-base"
9VIDEO_PATH = "path_to_a_test_sign_video.mp4"
10NUM_FRAMES = 16
11DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
12
13processor = VideoMAEImageProcessor.from_pretrained(MODEL_NAME)
14model = VideoMAEForVideoClassification.from_pretrained(
15 MODEL_NAME,
16 ignore_mismatched_sizes=True,
17)
18
19# Rebuild the sequential classifier head exactly as used in the original notebook.
20in_features = model.classifier.in_features
21NUM_CLASSES = model.config.num_labels
22model.classifier = nn.Sequential(
23 nn.LayerNorm(in_features),
24 nn.Dropout(0.3),
25 nn.Linear(in_features, NUM_CLASSES),
26)
27
28seq_ckpt_path = hf_hub_download(
29 repo_id=MODEL_NAME,
30 filename="classifier_sequential.pth",
31)
32seq_sd = torch.load(seq_ckpt_path, map_location="cpu", weights_only=True)
33model.load_state_dict(seq_sd, strict=False)
34
35model = model.to(DEVICE)
36model.eval()
37
38
39def load_video(video_path: str, num_frames: int = 16) -> list:
40 vr = VideoReader(video_path, ctx=cpu(0))
41 total = len(vr)
42 indices = np.linspace(0, total - 1, num_frames).astype(int)
43 frames = vr.get_batch(indices).asnumpy()
44 return list(frames)
45
46frames = load_video(VIDEO_PATH, num_frames=NUM_FRAMES)
47inputs = processor(frames, return_tensors="pt")
48inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
49
50with torch.no_grad():
51 outputs = model(**inputs)
52
53logits = outputs.logits
54pred_id = logits.argmax(-1).item()
55pred_label = model.config.id2label[pred_id]
56probs = torch.softmax(logits, dim=-1)[0]
57
58print(f"Predicted class : {pred_label}")
59print(f"Class ID : {pred_id}")
60print(f"Confidence : {probs[pred_id].item():.4f}")
61
62print("\nTop-5 predictions:")
63for rank, idx in enumerate(torch.argsort(probs, descending=True)[:5], 1):
64 idx = idx.item()
65 print(f" {rank}. [{idx:3d}] {model.config.id2label[idx]:<30s} {probs[idx].item():.4f}")