-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
146 lines (108 loc) · 4.14 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env rake
require "bundler/gem_tasks"
require_relative "lib/ocarina.rb"
require 'powerbar'
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << 'test'
end
desc "Run tests"
task :default => :test
namespace :ocarina do
include Ocarina::Util
desc "generates and persists bitmaps for reference/noise images"
task :bitmaps do |t, args|
# train/eval does not actually need bitmaps on disk- we provide this so that
# you can examine the generated bitmap images for debugging purposes.
#
# images are saved to data/images/{reference,noise}
generator = Ocarina::CharacterGenerator.new(config)
generator.persist_tiles
end
desc "builds and trains the network"
task :train do |t, args|
network = Ocarina::Network.new(config)
generator = Ocarina::CharacterGenerator.new(network.config)
training_iterations = 350
pbar = PowerBar.new
pbar.settings.tty.finite.template.barchar = '#'
pbar.settings.tty.finite.template.padchar = '-'
training_iterations.times do |i|
generator.reference_image_hash.each_pair do |char, tile|
network.train tile, char
pbar.show(msg: "current error: #{'%.10f' % network.current_error}", done: i + 1, total: training_iterations)
end
end
puts "\nfinal training error: #{network.current_error}\n"
# run with: ruby -I"lib:test" test/network_test.rb -n test_network
file = "#{Ocarina::DATA_DIR}/train.bin"
network.save_network_to_file file
puts "trained network saved to: #{file}"
end
desc "runs the training images back through the network for evaluation"
task :eval do |t, args|
file = "#{Ocarina::DATA_DIR}/train.bin"
network = Ocarina::Network.load_network_from_file file
generator = Ocarina::CharacterGenerator.new(network.config)
puts "##### testing against reference images #####"
stats = Ocarina::ErrorStats.new(network.config)
generator.reference_image_hash.each_pair do |char, tile|
result = network.recognize tile
stats.check_error char.ord, result
end
stats.report
puts
puts "##### testing against noise images #####"
stats = Ocarina::ErrorStats.new(network.config)
generator.noise_image_hash.each_pair do |char, tile|
result = network.recognize tile
stats.check_error char.ord, result
end
stats.report
end
desc "builds and trains network using Letterpress board tiles"
task :letterpress do |t, args|
network = Ocarina::Network.new(config)
generator = Ocarina::LetterpressCharacterGenerator.new(network.config)
training_iterations = 1200
pbar = PowerBar.new
pbar.settings.tty.finite.template.barchar = '#'
pbar.settings.tty.finite.template.padchar = '-'
training_iterations.times do |i|
generator.reference_image_hash.each_pair do |char, tile|
network.train tile, char
pbar.show(msg: "current error: #{'%.10f' % network.current_error}", done: i + 1, total: training_iterations)
end
end
puts "\nfinal training error: #{network.current_error}\n"
network.save_network_to_file "#{Ocarina::DATA_DIR}/letterpress-train.bin"
puts "trained network saved to: #{file}"
puts "##### testing against reference images #####"
stats = Ocarina::ErrorStats.new(network.config)
generator.reference_image_hash.each_pair do |char, tile|
result = network.recognize tile
stats.check_error char.ord, result
end
stats.report
end
desc "deciphers letters from a letterpress game board"
task :gameboard, [:board_file] do |t, args|
network = Ocarina::Network.load_network_from_file "#{Ocarina::DATA_DIR}/letterpress-train.bin"
puts "reading letterpress board: #{args.board_file}..."
board = Magick::Image.read(args.board_file).first
cropper = Ocarina::LetterpressCropper.new(network.config)
chars = cropper.decipher_board(network, board)
result = ""
chars.each do |row|
row.each do |char|
result << " #{char}"
end
result << "\n"
end
puts "result: \n\n#{result}"
end
def config
# need 8 bits to represent 0..255 in binary
@config ||= Ocarina::Config.new("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 8, 16, 16)
end
end