research log
Muzzle-Based Livestock Identification
Technical & Field Report
Broader Goal
Conventional livestock identification methods — ear-tagging, branding, and injectable RFID chips — are invasive, painful, and prone to tampering or loss. The goal of this project is to demonstrate that a photograph of an animal's muzzle is sufficient to uniquely identify an individual animal, replacing these methods with a non-invasive, camera-based biometric system.
This research has direct relevance to agricultural insurance programs, livestock traceability, and farmer-support initiatives run by programs such as UNDP in Nepal, where reliable cattle identification is critical to determining ownership and insurance eligibility.
My Role
This project is an active initiative at Plantsat, undertaken in collaboration with the United Nations Development Programme (UNDP) in Nepal. My contributions include:
- Designing and evaluating Siamese neural network architectures for muzzle-based identity verification
- Building the mobile data-collection application used across all field campaigns
- Co-designing the post-mortem data-collection protocol with the research team
- Conducting field testing and performance analysis across diverse farm environments
Technical Architecture
Core Approach: Siamese Neural Networks
The system is built on Siamese Neural Networks — a twin-branch architecture where both branches share identical weights. Given a pair of muzzle images, each branch independently generates a high-dimensional embedding vector. The Euclidean distance between the two embeddings determines whether the pair belongs to the same individual animal.
This approach is well-suited to livestock identification because:
- It does not require a fixed catalog of all animals at training time (open-set recognition)
- It generalizes to new, unseen animals without retraining
- It scales as more animals are enrolled, without modifying the model
Backbone Architectures Evaluated
We conducted a systematic comparative study across five state-of-the-art backbone architectures:
| Category | Backbone | Characteristics |
|---|---|---|
| CNN | VGG16 | Strong spatial feature extraction; higher parameter count |
| CNN | ResNet50 | Residual connections for better gradient flow |
| CNN | ResNet152 | Deeper variant; stronger feature representations |
| Transformer | ViT-Base | Global self-attention; class token output for identity embedding |
| Transformer | ViT-Large | Larger capacity; stronger global context modelling |
Convolutional backbones excel at capturing local spatial features such as individual groove patterns. Vision Transformers capture global structural context through self-attention — hypothesized to better represent overall muzzle topology.
Objective Function: Contrastive Loss
Embeddings are optimized using Contrastive Loss, which pulls same-identity pairs together and pushes different-identity pairs apart:
- d — Euclidean distance between the two embedding vectors
- Y = 0 for same-identity pairs (minimize distance, pull together)
- Y = 1 for different-identity pairs (push beyond the margin threshold)
- margin = 2.0, tuned from validation experiments
This forces the network to learn an embedding space where same-animal pairs cluster closely together while different-animal pairs are pushed beyond the margin threshold.
Dataset Construction
A key engineering contribution is the dynamic pair-sampling dataset class, which generates balanced positive (same identity) and negative (different identity) pairs during training. Without careful class balancing, the model encounters far more negative pairs, degrading performance. Our implementation maintains a controlled positive-to-negative ratio across every training batch.
Explainable AI: Grad-CAM Visualization
Deep learning models are often criticized as black boxes. To validate that our model is attending to the correct features, we implemented Gradient-weighted Class Activation Mapping (Grad-CAM):
- Forward and backward hooks are registered on the final convolutional or attention layer
- Gradients flowing back from the loss are averaged to produce per-channel importance weights
- These weights are applied to feature map activations and summed to produce a saliency map
- The saliency map is overlaid on the original input image as a visual heatmap
This lets us visually confirm that the network attends to anatomical muzzle groove patterns — the actual biometric signal — rather than background artifacts like fence posts, shadows, or farm environments. This interpretability step is essential for academic credibility and for demonstrating trustworthiness to UNDP program officers.
Field Findings: Why Single-Environment Data Is Not Enough
Farm Campaign
We conducted testing campaigns across multiple farms in Nepal under varying conditions — different lighting (morning, afternoon, overcast), different camera distances, different animal postures, and different environmental backgrounds.
Key Finding: Models trained on single-environment datasets failed to generalize across farms. A model trained on morning images from one farm performed poorly on afternoon images from another. This confirmed that multi-environment, multi-condition data collection is not optional — it is a core requirement for building a model that works reliably in deployment.
We updated our data collection strategy to systematically capture images across:
- Different times of day (varying light angle and colour temperature)
- Different weather conditions (sunny, overcast, rainy)
- Different farm backgrounds and enclosures
- Different camera angles and distances from the animal
Post-Mortem Muzzle Study: Slaughterhouse Collaboration
A specific challenge for livestock insurance programs is identifying dead cattle after slaughter — for example, to verify that the animal being claimed under an insurance policy is the same animal that was insured.
The muzzle changes significantly after death: it compresses, loses tissue hydration, and the groove patterns shift. To study this, we collaborated with a slaughterhouse and collected muzzle images at controlled time intervals:
| Time Point | Physiological Condition |
|---|---|
| 12 hours before slaughter | Baseline — fully alive and alert |
| 2 hours before slaughter | Mild pre-slaughter stress |
| Immediately before slaughter | High stress; muscle tension elevated |
| Immediately after slaughter | Immediate post-mortem; tissue still fresh |
| 2 hours after slaughter | Tissue compression visible; groove patterns shifted |
Training on this multi-interval dataset allowed the model to learn the trajectory of muzzle change over time, making it robust to post-mortem identification — a crucial capability for agricultural insurance verification.
Mobile Data Collection Application
To enable structured, large-scale data collection across all farm sites and time intervals, we built a dedicated mobile data-collection application using Flutter. The app:
- Guided field workers through a standardized capture workflow
- Tagged each image with structured metadata — farm ID, animal ID, lighting condition, and timestamp
- Operated fully offline-first with local persistence and automatic background sync when connectivity returned (critical in rural farm environments with poor signal)
- Integrated the on-device inference model so field workers could verify image quality and muzzle framing in real time
This application continues to be used in ongoing data collection campaigns as we scale the dataset to improve model accuracy.
Current Status & Next Stage
The training pipeline is functional across all five backbone architectures. Evaluation is conducted via a robust binary inference pipeline with tunable Euclidean distance thresholds, computing accuracy, precision, recall, F1-score, and confusion matrices for each backbone configuration.
The dataset collection is ongoing. This is a long-horizon research project — the model's accuracy continues to improve as more multi-environment and post-mortem data is added. Working with UNDP Nepal, the goal is to scale the dataset to cover a representative sample of livestock across Nepal's diverse agricultural regions.
Next engineering milestone: Optimize the best-performing backbone through:
- Model pruning — removing low-importance weights to reduce model size
- Quantization — reducing numerical precision (FP32 → INT8) for mobile deployment
- Knowledge distillation — training a lightweight student model from the larger teacher backbone
The end goal is real-time, edge inference on the mobile data-collection app itself — eliminating server-side inference dependency in low-connectivity farm environments.