Downloads · 30 days
0
shreyayayay/timesformer-dairy-cows
timesformer-dairy-cows is a video classification model from shreyayayay. Use it for the video classification task on the model card, and read the license before you ship it in a product. The card lists the license as mit.
Fine-tuned TimeSformer for seven-class behaviour recognition in dairy cattle, developed as part of the MooAnalytica digital twin framework at Dalhousie University.
Downloads · 30 days
0
Access
Public
Updated Jun 28, 2026
Repo size
1.5 GB
Likes
0
Public
Click a slice to open those files.
.pt1.5 GB · 100%
From the Hugging Face model README
Fine-tuned TimeSformer for seven-class behaviour recognition in dairy cattle, developed as part of the MooAnalytica digital twin framework at Dalhousie University.
This model forms the behavioural perception layer of an AI-driven dairy cattle digital twin, converting continuous barn surveillance video into structured per-cow behavioural state streams used for nutrition modelling and welfare monitoring.
facebook/timesformer-base-finetuned-k400 (Kinetics-400 pretrained)| ID | Label |
|---|---|
| 0 | Standing |
| 1 | Lying |
| 2 | Drinking |
| 3 | Feeding & Standing |
| 4 | Feeding & Lying |
| 5 | Ruminating & Standing |
| 6 | Ruminating & Lying |
Evaluated on the held-out validation set (n = 689 clips):
| Metric | Value |
|---|---|
| Overall accuracy | 85.8% |
| Macro-F1 | 0.836 |
| Daytime accuracy (n=670) | 88.2% |
| Nighttime accuracy (n=449) | 81.5% |
| mAP (macro) | 87.5% |
Per-class performance highlights: Feeding & Standing (F1=0.944), Lying (F1=0.912), Drinking (F1=0.905). Lowest: Standing (F1=0.720) and Ruminating & Standing (F1=0.782) due to postural similarity.
import torch
import numpy as np
from transformers import TimesformerForVideoClassification, AutoConfig
from huggingface_hub import hf_hub_download
# Load model
model_id = "shreyayayay/timesformer-dairy-cows"
config = AutoConfig.from_pretrained(model_id)
model = TimesformerForVideoClassification.from_pretrained(
model_id,
config=config,
ignore_mismatched_sizes=True
)
model.eval()
ID2LABEL = {
0: "Standing",
1: "Lying",
2: "Drinking",
3: "Feeding & Standing",
4: "Feeding & Lying",
5: "Ruminating & Standing",
6: "Ruminating & Lying",
}
IMNET_MEAN = np.array([0.485, 0.456, 0.406], dtype=np.float32)
IMNET_STD = np.array([0.229, 0.224, 0.225], dtype=np.float32)
def preprocess_clip(frames):
arr = np.stack(frames).astype(np.float32) / 255.0
arr = (arr - IMNET_MEAN) / IMNET_STD
arr = arr.transpose(0, 3, 1, 2)
return torch.from_numpy(arr).unsqueeze(0)
with torch.no_grad():
pixel_values = preprocess_clip(frames).to(next(model.parameters()).device)
logits = model(pixel_values=pixel_values).logits
pred = int(torch.argmax(logits, dim=-1))
print(f"Predicted behaviour: {ID2LABEL[pred]}")
import torch
from huggingface_hub import hf_hub_download
weights_path = hf_hub_download(
repo_id="shreyayayay/timesformer-dairy-cows",
filename="last.pt"
)
checkpoint = torch.load(weights_path, map_location="cpu")
Video → YOLOv11 detection → ByteTrack tracking → TimeSformer classification → Nutrition model (M-M DMI estimation)
Full pipeline code: github.com/raoshreya17/dairycow-digitaltwin
| Parameter | Value |
|---|---|
| Base model | facebook/timesformer-base-finetuned-k400 |
| Epochs | 6 (early stopping, patience=3) |
| Optimizer | AdamW (lr=2e-5, weight decay=1e-4) |
| Loss | Focal loss (γ=2, class-weighted) |
| Batch size | 8 (gradient accumulation ×4) |
| Mixed precision | fp16 |
| Hardware | NVIDIA A100 40GB |
| Framework | PyTorch 2.0, transformers==4.41.2 |
| Clip length | 12 frames @ 1.6 fps |
| Resolution | 224×224 px |
| Augmentation | Horizontal flip, brightness/contrast jitter, temporal jitter |
@article{rao2026mooanalytica,
title = {An AI-Driven Behavioural Perception and Nutrition Modelling Framework
as the Behavioural State Layer of a Dairy Cattle Digital Twin},
author = {Rao, Shreya and Garcia, Eduardo and Neethirajan, Suresh},
journal = {npj Veterinary Sciences},
volume = {1},
number = {3},
year = {2026},
publisher = {Nature Publishing Group},
doi = {10.1038/s44433-026-00004-x}
}
MIT — see LICENSE
Shreya Rao — MCS candidate, Dalhousie University
Supervisor: Dr. Suresh Neethirajan
GitHub: raoshreya17