Class: MachineLearningWorkbench::Compressor::VectorQuantization
- Inherits:
-
Object
- Object
- MachineLearningWorkbench::Compressor::VectorQuantization
- Defined in:
- lib/machine_learning_workbench/compressor/vector_quantization.rb
Constant Summary collapse
- Verification =
MachineLearningWorkbench::Tools::Verification
Instance Attribute Summary collapse
-
#centrs ⇒ Object
readonly
Returns the value of attribute centrs.
-
#dims ⇒ Object
readonly
Returns the value of attribute dims.
-
#dtype ⇒ Object
readonly
Returns the value of attribute dtype.
-
#lrate ⇒ Object
readonly
Returns the value of attribute lrate.
-
#ncentrs ⇒ Object
readonly
Returns the value of attribute ncentrs.
-
#rng ⇒ Object
readonly
Returns the value of attribute rng.
-
#vrange ⇒ Object
readonly
Returns the value of attribute vrange.
Instance Method Summary collapse
-
#initialize(ncentrs:, dims:, vrange:, dtype:, lrate:, rseed: Random.new_seed) ⇒ VectorQuantization
constructor
A new instance of VectorQuantization.
-
#most_similar_centr(img) ⇒ Object
Returns index and similitude of most similar centroid to image.
-
#new_centr ⇒ Object
Creates a new (random) centroid.
-
#reconstr_error(img) ⇒ Object
Per-pixel errors in reconstructing image.
-
#reconstruction(img) ⇒ Object
Reconstruct image as its most similar centroid.
-
#similarities(img) ⇒ Object
(also: #encode)
Computes similarities between image and all centroids.
-
#train(img_lst, debug: false) ⇒ Object
Train on image list.
-
#train_one(img, simils: nil) ⇒ Object
Train on one image.
Constructor Details
#initialize(ncentrs:, dims:, vrange:, dtype:, lrate:, rseed: Random.new_seed) ⇒ VectorQuantization
Returns a new instance of VectorQuantization.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/machine_learning_workbench/compressor/vector_quantization.rb', line 6 def initialize ncentrs:, dims:, vrange:, dtype:, lrate:, rseed: Random.new_seed @rng = Random.new rseed @ncentrs = ncentrs @dtype = dtype @dims = dims @lrate = lrate @vrange = case vrange when Array raise ArgumentError, "vrange size not 2: #{vrange}" unless vrange.size == 2 vrange.map &method(:Float) when Range [vrange.first, vrange.last].map &method(:Float) else raise ArgumentError, "vrange: unrecognized type: #{vrange.class}" end @centrs = ncentrs.times.map { new_centr } end |
Instance Attribute Details
#centrs ⇒ Object (readonly)
Returns the value of attribute centrs.
3 4 5 |
# File 'lib/machine_learning_workbench/compressor/vector_quantization.rb', line 3 def centrs @centrs end |
#dims ⇒ Object (readonly)
Returns the value of attribute dims.
3 4 5 |
# File 'lib/machine_learning_workbench/compressor/vector_quantization.rb', line 3 def dims @dims end |
#dtype ⇒ Object (readonly)
Returns the value of attribute dtype.
3 4 5 |
# File 'lib/machine_learning_workbench/compressor/vector_quantization.rb', line 3 def dtype @dtype end |
#lrate ⇒ Object (readonly)
Returns the value of attribute lrate.
3 4 5 |
# File 'lib/machine_learning_workbench/compressor/vector_quantization.rb', line 3 def lrate @lrate end |
#ncentrs ⇒ Object (readonly)
Returns the value of attribute ncentrs.
3 4 5 |
# File 'lib/machine_learning_workbench/compressor/vector_quantization.rb', line 3 def ncentrs @ncentrs end |
#rng ⇒ Object (readonly)
Returns the value of attribute rng.
3 4 5 |
# File 'lib/machine_learning_workbench/compressor/vector_quantization.rb', line 3 def rng @rng end |
#vrange ⇒ Object (readonly)
Returns the value of attribute vrange.
3 4 5 |
# File 'lib/machine_learning_workbench/compressor/vector_quantization.rb', line 3 def vrange @vrange end |
Instance Method Details
#most_similar_centr(img) ⇒ Object
Returns index and similitude of most similar centroid to image
40 41 42 43 44 45 |
# File 'lib/machine_learning_workbench/compressor/vector_quantization.rb', line 40 def most_similar_centr img simils = similarities img max_simil = simils.max max_idx = simils.index max_simil [max_idx, max_simil] end |
#new_centr ⇒ Object
Creates a new (random) centroid
25 26 27 |
# File 'lib/machine_learning_workbench/compressor/vector_quantization.rb', line 25 def new_centr NMatrix.new(dims, dtype: dtype) { rng.rand Range.new *vrange } end |
#reconstr_error(img) ⇒ Object
Per-pixel errors in reconstructing image
53 54 55 |
# File 'lib/machine_learning_workbench/compressor/vector_quantization.rb', line 53 def reconstr_error img reconstruction(img) - img end |
#reconstruction(img) ⇒ Object
Reconstruct image as its most similar centroid
48 49 50 |
# File 'lib/machine_learning_workbench/compressor/vector_quantization.rb', line 48 def reconstruction img centrs[most_similar_centr(img).first] end |
#similarities(img) ⇒ Object Also known as: encode
Computes similarities between image and all centroids
30 31 32 33 34 35 |
# File 'lib/machine_learning_workbench/compressor/vector_quantization.rb', line 30 def similarities img raise NotImplementedError if img.shape.size > 1 # centrs.map { |c| c.dot(img).first } require 'parallel' Parallel.map(centrs) { |c| c.dot(img).first } end |
#train(img_lst, debug: false) ⇒ Object
Train on image list
66 67 68 69 70 71 72 |
# File 'lib/machine_learning_workbench/compressor/vector_quantization.rb', line 66 def train img_lst, debug: false # Two ways here: # - Batch: canonical, centrs updated with each img # - Parallel: could be parallel either on simils or on training (?) # Unsure on the correctness of either Parallel, let's stick with Batch img_lst.each { |img| train_one img; print '.' if debug } end |
#train_one(img, simils: nil) ⇒ Object
Train on one image
58 59 60 61 62 63 |
# File 'lib/machine_learning_workbench/compressor/vector_quantization.rb', line 58 def train_one img, simils: nil trg_idx, _simil = simils || most_similar_centr(img) centrs[trg_idx] = centrs[trg_idx] * (1-lrate) + img * lrate Verification.in_range! centrs[trg_idx], vrange centrs[trg_idx] end |