Class: Gsplat::Strategy::Default

Inherits:
Base
  • Object
show all
Defined in:
lib/gsplat/strategy/default.rb

Overview

Original 3DGS densification strategy with gsplat refinements.

Constant Summary collapse

DEFAULTS =

Upstream-compatible densification defaults.

{
  prune_opa: 0.005,
  grow_grad2d: 0.0002,
  grow_scale3d: 0.01,
  grow_scale2d: 0.05,
  prune_scale3d: 0.1,
  prune_scale2d: 0.15,
  refine_scale2d_stop_iter: 0,
  refine_start_iter: 500,
  refine_stop_iter: 15_000,
  reset_every: 3_000,
  refine_every: 100,
  pause_refine_after_reset: 0,
  absgrad: false,
  revised_opacity: false,
  key_for_gradient: :means2d
}.freeze

Constants inherited from Base

Base::REQUIRED_KEYS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#check_sanity, #step_pre_backward

Constructor Details

#initialize(**options) ⇒ Default

Returns a new instance of Default.

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
# File 'lib/gsplat/strategy/default.rb', line 32

def initialize(**options)
  super()
  unknown = options.keys - DEFAULTS.keys
  raise ArgumentError, "unknown options: #{unknown.join(', ')}" unless unknown.empty?

  DEFAULTS.merge(options).each { |name, value| instance_variable_set(:"@#{name}", value) }
  validate_options!
end

Instance Attribute Details

#absgradObject (readonly)

Returns the value of attribute absgrad.



26
27
28
# File 'lib/gsplat/strategy/default.rb', line 26

def absgrad
  @absgrad
end

#grow_grad2dObject (readonly)

Returns the value of attribute grow_grad2d.



26
27
28
# File 'lib/gsplat/strategy/default.rb', line 26

def grow_grad2d
  @grow_grad2d
end

#grow_scale2dObject (readonly)

Returns the value of attribute grow_scale2d.



26
27
28
# File 'lib/gsplat/strategy/default.rb', line 26

def grow_scale2d
  @grow_scale2d
end

#grow_scale3dObject (readonly)

Returns the value of attribute grow_scale3d.



26
27
28
# File 'lib/gsplat/strategy/default.rb', line 26

def grow_scale3d
  @grow_scale3d
end

#key_for_gradientObject (readonly)

Returns the value of attribute key_for_gradient.



26
27
28
# File 'lib/gsplat/strategy/default.rb', line 26

def key_for_gradient
  @key_for_gradient
end

#pause_refine_after_resetObject (readonly)

Returns the value of attribute pause_refine_after_reset.



26
27
28
# File 'lib/gsplat/strategy/default.rb', line 26

def pause_refine_after_reset
  @pause_refine_after_reset
end

#prune_opaObject (readonly)

Returns the value of attribute prune_opa.



26
27
28
# File 'lib/gsplat/strategy/default.rb', line 26

def prune_opa
  @prune_opa
end

#prune_scale2dObject (readonly)

Returns the value of attribute prune_scale2d.



26
27
28
# File 'lib/gsplat/strategy/default.rb', line 26

def prune_scale2d
  @prune_scale2d
end

#prune_scale3dObject (readonly)

Returns the value of attribute prune_scale3d.



26
27
28
# File 'lib/gsplat/strategy/default.rb', line 26

def prune_scale3d
  @prune_scale3d
end

#refine_everyObject (readonly)

Returns the value of attribute refine_every.



26
27
28
# File 'lib/gsplat/strategy/default.rb', line 26

def refine_every
  @refine_every
end

#refine_scale2d_stop_iterObject (readonly)

Returns the value of attribute refine_scale2d_stop_iter.



26
27
28
# File 'lib/gsplat/strategy/default.rb', line 26

def refine_scale2d_stop_iter
  @refine_scale2d_stop_iter
end

#refine_start_iterObject (readonly)

Returns the value of attribute refine_start_iter.



26
27
28
# File 'lib/gsplat/strategy/default.rb', line 26

def refine_start_iter
  @refine_start_iter
end

#refine_stop_iterObject (readonly)

Returns the value of attribute refine_stop_iter.



26
27
28
# File 'lib/gsplat/strategy/default.rb', line 26

def refine_stop_iter
  @refine_stop_iter
end

#reset_everyObject (readonly)

Returns the value of attribute reset_every.



26
27
28
# File 'lib/gsplat/strategy/default.rb', line 26

def reset_every
  @reset_every
end

#revised_opacityObject (readonly)

Returns the value of attribute revised_opacity.



26
27
28
# File 'lib/gsplat/strategy/default.rb', line 26

def revised_opacity
  @revised_opacity
end

Instance Method Details

#initialize_state(scene_scale:) ⇒ Hash

Creates mutable gradient/radius statistics for a scene.

Parameters:

  • scene_scale (Numeric)

Returns:

  • (Hash)


45
46
47
# File 'lib/gsplat/strategy/default.rb', line 45

def initialize_state(scene_scale:)
  super.merge(grad2d: nil, count: nil, radii: nil)
end

#refinement_masks(params, state) ⇒ Object

rubocop:disable Metrics/AbcSize



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gsplat/strategy/default.rb', line 61

def refinement_masks(params, state)
  count = params.fetch(:means).data.shape[0]
  average = params.fetch(:means).data.class.zeros(count)
  observed = state[:count].gt(0)
  average[observed] = state[:grad2d][observed] / state[:count][observed] if observed.any?
  high = average.gt(grow_grad2d)
  max_scale = Numo::NMath.exp(params.fetch(:scales).data).max(axis: 1)
  small = max_scale.le(grow_scale3d * state.fetch(:scene_scale))
  small &= state[:radii].le(grow_scale2d) if refine_scale2d_stop_iter.positive? && state[:radii]
  [high & small, high & small.eq(0)]
end

#step_post_backward(params:, optimizers:, state:, step:, info:, **_options) ⇒ Object

rubocop:disable Metrics/ParameterLists



50
51
52
53
54
55
56
57
# File 'lib/gsplat/strategy/default.rb', line 50

def step_post_backward(params:, optimizers:, state:, step:, info:, **_options)
  check_sanity(params, optimizers)
  collect_statistics!(params, state, info)
  refine!(params, optimizers, state, step) if should_refine?(step)
  reset_due = step.positive? && (step % reset_every).zero?
  Ops.reset_opacity!(params, optimizers, maximum: 2 * prune_opa) if reset_due
  state
end