Class: CooCoo::DataSources::Xournal::BitmapStream
- Defined in:
- lib/coo-coo/data_sources/xournal/bitmap_stream.rb
Instance Attribute Summary collapse
-
#canvas_klass ⇒ Object
Returns the value of attribute canvas_klass.
-
#example_height ⇒ Object
readonly
Returns the value of attribute example_height.
-
#example_width ⇒ Object
readonly
Returns the value of attribute example_width.
-
#labels ⇒ Object
readonly
Returns the value of attribute labels.
-
#pen_scale ⇒ Object
Returns the value of attribute pen_scale.
-
#shuffle ⇒ Object
Returns the value of attribute shuffle.
-
#training_documents ⇒ Object
readonly
Returns the value of attribute training_documents.
-
#use_color ⇒ Object
readonly
Returns the value of attribute use_color.
Instance Method Summary collapse
- #add_label(label) ⇒ Object
- #add_training_document(path_or_td) ⇒ Object
- #decode_output(output) ⇒ Object
- #each(yield_canvas = false, &block) ⇒ Object
- #encode_label(label) ⇒ Object
- #encode_strokes(strokes, return_canvas = false) ⇒ Object
- #encode_strokes_to_canvas(strokes, canvas) ⇒ Object
-
#initialize(options = Hash.new) ⇒ BitmapStream
constructor
A new instance of BitmapStream.
- #input_size ⇒ Object
- #output_size ⇒ Object
- #process_training_document(td) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(options = Hash.new) ⇒ BitmapStream
Returns a new instance of BitmapStream.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 19 def initialize( = Hash.new) @training_documents = Array.new @document_paths = Array.new @pen_scale = .fetch(:pen_scale, 1.0) @example_width = .fetch(:width, 28) @example_height = .fetch(:height, 28) @num_labels = [:num_labels] if [:labels] @labels = File.read([:labels]).split("\n") else @labels = Array.new end @canvas_klass = .fetch(:canvas, Drawing::CairoCanvas) @use_color = .fetch(:use_color, false) @shuffle = .fetch(:shuffle, 16) [:training_documents].each do |td| add_training_document(td) end end |
Instance Attribute Details
#canvas_klass ⇒ Object
Returns the value of attribute canvas_klass.
14 15 16 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 14 def canvas_klass @canvas_klass end |
#example_height ⇒ Object (readonly)
Returns the value of attribute example_height.
13 14 15 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 13 def example_height @example_height end |
#example_width ⇒ Object (readonly)
Returns the value of attribute example_width.
13 14 15 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 13 def example_width @example_width end |
#labels ⇒ Object (readonly)
Returns the value of attribute labels.
12 13 14 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 12 def labels @labels end |
#pen_scale ⇒ Object
Returns the value of attribute pen_scale.
15 16 17 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 15 def pen_scale @pen_scale end |
#shuffle ⇒ Object
Returns the value of attribute shuffle.
17 18 19 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 17 def shuffle @shuffle end |
#training_documents ⇒ Object (readonly)
Returns the value of attribute training_documents.
11 12 13 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 11 def training_documents @training_documents end |
#use_color ⇒ Object (readonly)
Returns the value of attribute use_color.
16 17 18 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 16 def use_color @use_color end |
Instance Method Details
#add_label(label) ⇒ Object
75 76 77 78 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 75 def add_label(label) @labels << label unless @labels.find_index(label) self end |
#add_training_document(path_or_td) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 40 def add_training_document(path_or_td) td = case path_or_td when String then TrainingDocument.from_file(path_or_td) when Pathname then TrainingDocument.from_file(path_or_td.to_s) when TrainingDocument then path_or_td else raise ArgumentError.new("#{path_or_td.inspect} is not a String, Pathname, or TrainingDocument") end process_training_document(td) @document_paths << path_or_td unless td == path_or_td @training_documents << td self end |
#decode_output(output) ⇒ Object
87 88 89 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 87 def decode_output(output) @labels[output.each.with_index.max[1]] end |
#each(yield_canvas = false, &block) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 117 def each(yield_canvas = false, &block) return to_enum(__method__, yield_canvas) unless block_given? training_documents.each do |td| stroke_set = 0 loop do td.each_example.each_slice(shuffle) do |slice| examples = slice.collect do |ex| strokes = ex.stroke_sets[stroke_set] [ ex.label, strokes ] unless strokes.nil? || strokes.empty? end.reject(&:nil?) raise StopIteration if examples.empty? examples.shuffle.each do |(label, strokes)| yield(encode_label(label), encode_strokes(strokes, yield_canvas)) end end stroke_set += 1 end end end |
#encode_label(label) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 80 def encode_label(label) i = @labels.find_index(label) v = Vector.zeros(output_size) v[i] = 1.0 v end |
#encode_strokes(strokes, return_canvas = false) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 102 def encode_strokes(strokes, return_canvas = false) canvas = @canvas_klass.new(@example_width, @example_height) if pen_scale != 1.0 strokes = strokes.collect { |s| s.scale(1.0, 1.0, pen_scale) } end encode_strokes_to_canvas(strokes, canvas) if return_canvas canvas.flush else canvas.to_vector(!@use_color) / 256.0 end end |
#encode_strokes_to_canvas(strokes, canvas) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 91 def encode_strokes_to_canvas(strokes, canvas) canvas.fill_color = 'white' canvas.stroke_color = 'white' canvas.rect(0, 0, @example_width, @example_height) ren = Renderer.new strokes.each do |stroke| ren.render_stroke(canvas, stroke, 0, 0, 1, 1, @example_width, @example_height) end end |
#input_size ⇒ Object
67 68 69 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 67 def input_size example_width * example_height * (@use_color ? 3 : 1) end |
#output_size ⇒ Object
71 72 73 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 71 def output_size Math.max(@labels.size, @num_labels) end |
#process_training_document(td) ⇒ Object
53 54 55 56 57 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 53 def process_training_document(td) td.labels.each do |l| add_label(l) end end |
#size ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/coo-coo/data_sources/xournal/bitmap_stream.rb', line 59 def size training_documents.reduce(0) do |total, td| total + td.each_example.reduce(0) do |subtotal, ex| subtotal + ex.size end end end |