Documentation
¶
Overview ¶
Package depth estimates a video's depth, frame by frame: Video Depth Anything (Chen et al., CVPR 2025), its streaming form, from the published checkpoint as it is. A frame goes through a DINOv2 vision transformer; a DPT head makes a depth map of its features, with four temporal attention modules in it that look at the frames before (their states cached: one frame in, one depth map out).
Index ¶
- Variables
- func BuiltinTail() map[string]*nn.Tensor
- func DecodeTail(r io.Reader) (map[string]*nn.Tensor, error)
- func Encoder(o Ops, p Params, x *nn.Tensor) [4]*nn.Tensor
- func PositionEncoding(w map[string]*nn.Tensor, block string, c int) *nn.Tensor
- func ReadTail(path string) (map[string]*nn.Tensor, error)
- func StartWindow(states [][8]KV) [][8]KV
- func Tail(o Ops, p Params, in TailInput) *nn.Tensor
- func WriteTail(w io.Writer, weights map[string]*nn.Tensor) error
- type KV
- type Model
- func (m *Model) Arch() (blocks, heads, dim int)
- func (m *Model) Backend() nn.Backend
- func (m *Model) Encode(x *nn.Tensor) [4]*nn.Tensor
- func (m *Model) Frame(x *nn.Tensor) *nn.Tensor
- func (m *Model) FrameErr(x *nn.Tensor) (*nn.Tensor, error)
- func (m *Model) HalfSafe() bool
- func (m *Model) Infer(x *nn.Tensor) *nn.Tensor
- func (m *Model) Opt(name string) *nn.Tensor
- func (m *Model) PE(block string, c, t int) [3]*nn.Tensor
- func (m *Model) Pos(ph, pw int) *nn.Tensor
- func (m *Model) Reset()
- func (m *Model) SetTail(w map[string]*nn.Tensor) error
- func (m *Model) Size() string
- func (m *Model) T(name string) *nn.Tensor
- func (m *Model) Use(be nn.Backend)
- func (m *Model) Weights() map[string]*nn.Tensor
- type Ops
- type Params
- type TailInput
Constants ¶
This section is empty.
Variables ¶
var TailNames = []string{
"head.projects.0", "head.resize_layers.0", "head.scratch.layer1_rn",
"head.projects.1", "head.resize_layers.1", "head.scratch.layer2_rn",
"head.scratch.refinenet2.resConfUnit1.conv1", "head.scratch.refinenet2.resConfUnit1.conv2",
"head.scratch.refinenet2.resConfUnit2.conv1", "head.scratch.refinenet2.resConfUnit2.conv2",
"head.scratch.refinenet2.out_conv",
"head.scratch.refinenet1.resConfUnit1.conv1", "head.scratch.refinenet1.resConfUnit1.conv2",
"head.scratch.refinenet1.resConfUnit2.conv1", "head.scratch.refinenet1.resConfUnit2.conv2",
"head.scratch.refinenet1.out_conv",
"head.scratch.output_conv1", "head.scratch.output_conv2.0", "head.scratch.output_conv2.2",
}
TailNames is the head's tail (each a layer's .weight and, when it has one, .bias), which TailInput feeds and which can be learnt anew (tools/depthtrain -tail): the first two features' projections and resizing, their convolutions into the fusion, the last two fusion blocks and the output convolutions.
Functions ¶
func BuiltinTail ¶
BuiltinTail is the built-in tail's weights (SetTail's), for Video Depth Anything Small.
func DecodeTail ¶
DecodeTail reads a tail's weights from r.
func Encoder ¶ added in v0.19.0
Encoder runs DINOv2 on o with p: x the frame as the network takes it ([3, H, W], o's tensor); the patch tokens of the intermediate blocks' outputs, normed ([ph pw, D] each).
func PositionEncoding ¶ added in v0.19.0
PositionEncoding is an attention block's sinusoidal position encoding ([frames, C]): the checkpoint's (w's), else made as the reference's PositionalEncoding makes it.
func StartWindow ¶ added in v0.19.0
StartWindow is the window a stream's frame sees when states are the frames' before it (oldest first), as Infer builds it from the frames it has seen: nil for a stream's first frame.
func Tail ¶ added in v0.19.0
Tail runs the head's tail (TailNames' layers) on its inputs: the depth at the patch grid times 14 ([1, H, W]), as Head gives it.
Types ¶
type KV ¶ added in v0.19.0
KV is a frame's keys and values for a temporal attention block ([D, C] each, D the positions), less the position encoding's part.
func Head ¶ added in v0.19.0
func Head(o Ops, p Params, feats [4]*nn.Tensor, ph, pw int, win [][8]KV, tap func(TailInput)) (*nn.Tensor, [8]KV)
Head runs the DPT head on a frame's encoder features (feats, [ph pw, C] each) with the window of the frames before it (win, oldest first; see StartWindow): the depth at the patch grid times 14 ([1, H, W], Infer's before its final resize) and the frame's eight temporal states. tap, when not nil, is given the tail's inputs.
type Model ¶
type Model struct {
// Tap, when set, is given each frame's head inputs to its trainable
// tail (TailInput), on the backend: for learning the tail.
Tap func(TailInput)
// contains filtered or unexported fields
}
Model is a loaded Video Depth Anything and its stream's state.
func Load ¶
Load reads a checkpoint (video_depth_anything_vits.pth, or another model of the same make).
func New ¶
New is a model of loaded weights. Its encoder's attention heads are not in them: every DINOv2's are 64 channels each (the small model's 384 in 6), a model narrower than 64's 12 (the tests').
func (*Model) Encode ¶ added in v0.19.0
Encode runs the encoder on a frame x ([3, H, W], the backend's): its features for the head ([ph pw, C] each, the backend's; see Head).
func (*Model) Frame ¶
Frame gives the depth of the next frame of the stream: x is the frame as the network takes it ([3, H, W], H and W multiples of 14, the colours normalised by ImageNet's mean and deviation); the depth is [H, W], a relative inverse depth (larger nearer). It panics on the backend's failure (a GPU's); FrameErr reports it.
func (*Model) HalfSafe ¶
HalfSafe reports whether the model's matrix products may take their inputs in half precision (nn.GPU's Half): Small's and Base's lose nothing measurable; Large's activations overflow it (NaN on glass).
func (*Model) Infer ¶
Infer is Frame on the backend: x the backend's tensor, the depth [1, H, W] the backend's, the intermediates not released.
func (*Model) PE ¶ added in v0.19.0
PE is a temporal attention block's projected position encodings for a window of t frames (Params), computed once a window length and kept on the backend.
func (*Model) Pos ¶ added in v0.19.0
Pos is the encoder's position embedding for a grid of patches, the backend's (Params).
func (*Model) Reset ¶
func (m *Model) Reset()
Reset forgets the frames before: the next frame starts a new stream (a new shot, say).
func (*Model) SetTail ¶
SetTail puts a tail's weights (ReadTail's) in place of the checkpoint's: each must be one the model has, of the same shape.
func (*Model) Size ¶
Size is the model's: "small", "base" or "large" (by its encoder's width; "" for another, the tests' small random ones), each its own scale of depth.
type Ops ¶ added in v0.19.0
type Ops interface {
Upload(t *nn.Tensor) *nn.Tensor
Conv2d(x, w, b *nn.Tensor, stride, pad int) *nn.Tensor
ConvTranspose2dK(x, w, b *nn.Tensor) *nn.Tensor
Linear(x, w, b *nn.Tensor) *nn.Tensor
LayerNorm(x, w, b *nn.Tensor, eps float32) *nn.Tensor
GroupNorm(x *nn.Tensor, groups int, w, b *nn.Tensor, eps float32) *nn.Tensor
GEGLU(x *nn.Tensor) *nn.Tensor
ReLU(x *nn.Tensor) *nn.Tensor
Add(a, b *nn.Tensor) *nn.Tensor
Clone(x *nn.Tensor) *nn.Tensor
Bilinear(x *nn.Tensor, oh, ow int) *nn.Tensor
Transpose(x *nn.Tensor) *nn.Tensor
TemporalAttention(q, peQ, peK, peV *nn.Tensor, ks, vs []*nn.Tensor, heads int, scale float32) *nn.Tensor
// the encoder's
Attention(q, k, v *nn.Tensor, heads int, scale float32) *nn.Tensor
GELU(x *nn.Tensor) *nn.Tensor
AddScaled(x, y, s *nn.Tensor) *nn.Tensor
Tokens(patches, cls, pos *nn.Tensor) *nn.Tensor
Split3(x *nn.Tensor) (*nn.Tensor, *nn.Tensor, *nn.Tensor)
Rows(x *nn.Tensor, from int) *nn.Tensor
}
Ops are the layers the head is made of: a backend's (every nn.Backend has them) or an nn.Tape's. The head uses what each op gives, never counting on one being in place, and gives the ops that are in place on a backend (ReLU, Add) nothing it still needs.
type Params ¶ added in v0.19.0
type Params interface {
T(name string) *nn.Tensor
Opt(name string) *nn.Tensor
PE(block string, c, t int) [3]*nn.Tensor
Pos(ph, pw int) *nn.Tensor
Arch() (blocks, heads, dim int)
}
Params are the network's weights, as the ops' tensors: T a weight by its checkpoint name, Opt one that may be absent (a bias: nil then), PE a temporal attention block's position encodings projected by its query, key and value weights, for a window of t frames: the query's (the last place's, [C]) and the keys' and values' ([t, C]); Pos the encoder's position embedding for a grid of patches ([1 + ph pw, D], the class token's first), and Arch the encoder's depth, heads and width.
func TapeParams ¶ added in v0.19.0
TapeParams are Params on an nn.Tape for learning: the weights w (in memory, by their checkpoint names), those learn says of as the tape's learnt weights (their gradients gathered under their names), the rest uploaded as they are; the position encodings' projections computed on the tape, so the gradients reach the attention weights through them too. They serve one pass: the tape's tensors go at its Release.
type TailInput ¶
type TailInput struct {
Feat0, Feat1, P3 *nn.Tensor
H, W int
Input *nn.Tensor
Fresh bool // the stream's first frame (after Reset): the frames before are not its window's
}
TailInput is what the head's tail (Tail) takes: the encoder's first two features as maps ([C, ph, pw]: the projections' inputs), and the fusion's third stage after its temporal module ([F, H, W]), with the network's input size (the depth's). Tap is also given the frame as the network took it (Input, [3, H, W]; Head alone leaves it nil).