Skip to main content

Tutorial: Using a Hidden Markov Model to Predict Behavioral Epochs with Deep Learning Features

Overview

This tutorial walks through a complete workflow for classifying behavioral epochs in video data using deep learning feature extraction and a Hidden Markov Model (HMM). The pipeline is:

  1. Extract per-frame features — Run a pre-trained encoder (e.g. ConvNeXt [paper]) over each video frame to produce a compact feature vector.
  2. Reduce dimensionality — Apply post-encoder processing (global average pooling or spatial point extraction) to collapse the spatial feature map into a 1D feature vector per frame.
  3. Train an HMM — Use the feature time series and a set of labeled epochs to fit a Hidden Markov Model that captures the temporal structure of behavioral states.
  4. Predict epochs — Run the trained HMM on unlabeled frames to automatically segment the video into behavioral epochs.

By the end you will have a repeatable pipeline that turns raw video into labeled behavioral intervals stored as DigitalIntervalSeries in the DataManager.

Prerequisites

  • A video loaded in the Media Viewer.
  • A pre-trained encoder model exported as .pt2 (AOT Inductor) or .pt (TorchScript). See Exporting an AOTI Encoder Model for how to export a ConvNeXt backbone.
  • (Optional) Labeled behavioral intervals for training the HMM. These can be created using the Label Video Intervals workflow.

Pipeline Overview

Video Frames
     │
     ▼
┌─────────────┐
│   Encoder   │  ConvNeXt / ViT / ResNet
│  (forward)  │  [B, 3, 224, 224] → [B, 768, 7, 7]
└─────┬───────┘
      │
      ▼
┌─────────────────────┐
│ Post-Encoder Module │  Global Avg Pool or Spatial Point Extraction
│                     │  [B, 768, 7, 7] → [B, 768]
└─────┬───────────────┘
      │
      ▼
┌─────────────┐
│ TensorData  │  One 768-dim feature vector per frame
│ (stored in  │  stored in DataManager
│ DataManager)│
└─────┬───────┘
      │
      ▼
┌─────────────┐
│    HMM      │  Trained on labeled intervals
│  (predict)  │  Outputs state sequence
└─────┬───────┘
      │
      ▼
┌───────────────-───────┐
│ DigitalIntervalSeries │  Behavioral epoch labels
│ (stored in            │  per frame
│  DataManager)         │
└───────────────────────┘

Step 1: Export Your Encoder Model

Follow the Exporting an AOTI Encoder Model how-to guide to export a ConvNeXt-Tiny backbone. You will end up with:

  • convnext_encoder.pt2 — the compiled model weights
  • convnext_encoder.json — the RuntimeModelSpec that describes the model’s I/O

Step 2: Load the Encoder in Neuralyzer

This section will be expanded once the GeneralEncoderModel and post-encoder modules are implemented.

  1. Open View → Tools → Deep Learning.
  2. Load the convnext_encoder.json model spec.
  3. Browse to the .pt2 weights file.
  4. Bind the "image" input slot to your video media data.
  5. Configure a post-encoder module (Global Average Pooling) to reduce the feature map.
  6. Bind the output to a TensorData key (e.g. "encoder_features").

Step 3: Extract Features Over the Video

This section will be expanded once batch inference with post-encoder modules is wired up.

Run batch inference over all frames (or a frame range). Each frame produces a feature vector stored in the "encoder_features" TensorData object.

Step 4: Label Training Epochs

If you don’t already have labeled behavioral intervals:

  1. Use the Label Video Intervals workflow to mark epochs of distinct behaviors (e.g. “grooming”, “resting”, “locomotion”).
  2. Store them as DigitalIntervalSeries objects in the DataManager.

These labeled intervals serve as the training signal for the HMM.

Step 5: Train the HMM

This section will be expanded once the MLCore HMM integration is available.

Step 6: Predict Behavioral Epochs

This section will be expanded once the HMM prediction pipeline is complete.

Next Steps

  • Evaluate prediction accuracy using held-out labeled data.
  • Experiment with different encoders (ViT, ResNet) and post-encoder modules (spatial point extraction at a landmark location vs. global average pooling).
  • Fine-tune the number of HMM states and observation model.