depth

package
v0.21.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

View Source
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

func BuiltinTail() map[string]*nn.Tensor

BuiltinTail is the built-in tail's weights (SetTail's), for Video Depth Anything Small.

func DecodeTail

func DecodeTail(r io.Reader) (map[string]*nn.Tensor, error)

DecodeTail reads a tail's weights from r.

func Encoder added in v0.19.0

func Encoder(o Ops, p Params, x *nn.Tensor) [4]*nn.Tensor

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

func PositionEncoding(w map[string]*nn.Tensor, block string, c int) *nn.Tensor

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 ReadTail

func ReadTail(path string) (map[string]*nn.Tensor, error)

ReadTail reads a tail's weights (WriteTail's).

func StartWindow added in v0.19.0

func StartWindow(states [][8]KV) [][8]KV

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

func Tail(o Ops, p Params, in TailInput) *nn.Tensor

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.

func WriteTail

func WriteTail(w io.Writer, weights map[string]*nn.Tensor) error

WriteTail writes a tail's weights: the magic, their count, then each one's name (its length, uint32, then its bytes), its dimensions (their count, then each, uint32) and its values (float32), little-endian, in name order.

Types

type KV added in v0.19.0

type KV struct{ K, V *nn.Tensor }

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(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

func Load(file string) (*Model, error)

Load reads a checkpoint (video_depth_anything_vits.pth, or another model of the same make).

func New

func New(w map[string]*nn.Tensor) (*Model, error)

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) Arch added in v0.19.0

func (m *Model) Arch() (blocks, heads, dim int)

Arch is the encoder's depth, heads and width (Params).

func (*Model) Backend

func (m *Model) Backend() nn.Backend

Backend is the backend the model runs on.

func (*Model) Encode added in v0.19.0

func (m *Model) Encode(x *nn.Tensor) [4]*nn.Tensor

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

func (m *Model) Frame(x *nn.Tensor) *nn.Tensor

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) FrameErr

func (m *Model) FrameErr(x *nn.Tensor) (*nn.Tensor, error)

FrameErr is Frame, the backend's failure an error.

func (*Model) HalfSafe

func (m *Model) HalfSafe() bool

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

func (m *Model) Infer(x *nn.Tensor) *nn.Tensor

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) Opt added in v0.19.0

func (m *Model) Opt(name string) *nn.Tensor

Opt is a weight that may be absent (Params).

func (*Model) PE added in v0.19.0

func (m *Model) PE(block string, c, t int) [3]*nn.Tensor

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

func (m *Model) Pos(ph, pw int) *nn.Tensor

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

func (m *Model) SetTail(w map[string]*nn.Tensor) error

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

func (m *Model) Size() string

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.

func (*Model) T added in v0.19.0

func (m *Model) T(name string) *nn.Tensor

T is a weight on the backend (Params).

func (*Model) Use

func (m *Model) Use(be nn.Backend)

Use runs the model on a backend (nn.CPU at first; an nn.GPU, say): the weights go there as they are needed. It starts a new stream.

func (*Model) Weights added in v0.19.0

func (m *Model) Weights() map[string]*nn.Tensor

Weights are the model's, in memory, by their checkpoint names.

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

func TapeParams(tp *nn.Tape, w map[string]*nn.Tensor, learn func(name string) bool) Params

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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL