treyja 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/README.md +8 -23
- data/exe/treyja +2 -208
- data/lib/treyja/command/csv.rb +57 -0
- data/lib/treyja/command/dump.rb +18 -0
- data/lib/treyja/command/help.rb +25 -0
- data/lib/treyja/command/image.rb +104 -0
- data/lib/treyja/command/json.rb +18 -0
- data/lib/treyja/exe.rb +81 -0
- data/lib/{tensor_pb.rb → treyja/proto/tensor_pb.rb} +0 -0
- data/lib/{tensors_pb.rb → treyja/proto/tensors_pb.rb} +1 -1
- data/lib/treyja/reader.rb +25 -0
- data/lib/treyja/version.rb +1 -1
- metadata +11 -5
- data/treyja/.gitignore +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 126534d5a966eec091dd2542c8e7fe0d70e5c483ca42324dba7872504976545b
|
4
|
+
data.tar.gz: 94e2b98f05650a6d0212ed795a43864797bf357ffaad6227537f6b3b44c768df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8a4bfb3224a2e2ebf0b5dacb9263e75e281487411c6edde32f61a5464f4969773448dad0e309e27295e628082aac62833a6ed893317352949bd3c30ea71cfd2
|
7
|
+
data.tar.gz: 2cd1465e1e3cd403ba74c2b260c6d7cac9c20da4d072828f034975cd722fd30596e66ed8f3040e5216edc75d28c828ff1cb6f9cbeac019a48c58fb2f663fd41f
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
treyja (0.1.
|
4
|
+
treyja (0.1.3)
|
5
5
|
google-protobuf (~> 3.6)
|
6
6
|
|
7
7
|
GEM
|
@@ -9,7 +9,7 @@ GEM
|
|
9
9
|
specs:
|
10
10
|
coderay (1.1.2)
|
11
11
|
diff-lcs (1.3)
|
12
|
-
google-protobuf (3.7.1)
|
12
|
+
google-protobuf (3.7.1-universal-darwin)
|
13
13
|
method_source (0.9.2)
|
14
14
|
pry (0.12.0)
|
15
15
|
coderay (~> 1.1.0)
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Treyja is a Ruby script which takes `Tensors` protobin and outputs in human readable way.
|
4
4
|
|
5
|
-
##
|
5
|
+
## Installation
|
6
6
|
|
7
7
|
Make sure you have `ruby` and `gem` commands installed.
|
8
8
|
|
@@ -13,34 +13,19 @@ $ gem --version
|
|
13
13
|
2.7.6
|
14
14
|
```
|
15
15
|
|
16
|
-
|
16
|
+
Install it yourself as:
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
```console
|
21
|
-
$ gem install specific_install
|
22
|
-
$ gem specific_install https://github.com/xc-jp/treyja-ruby.git
|
23
|
-
```
|
24
|
-
|
25
|
-
You can check if `treyja` is properly installed by:
|
26
|
-
|
27
|
-
```console
|
28
|
-
% gem list treyja
|
29
|
-
|
30
|
-
*** LOCAL GEMS ***
|
31
|
-
|
32
|
-
treyja (0.1.0)
|
33
|
-
```
|
18
|
+
$ gem install treyja
|
34
19
|
|
35
20
|
|
36
21
|
## How to use
|
37
22
|
|
38
|
-
Assume that we are in the root dir of `alderaan
|
23
|
+
Assume that we are in the root dir of `alderaan`.
|
39
24
|
|
40
25
|
### To show in JSON format
|
41
26
|
|
42
27
|
```console
|
43
|
-
$ draupnir get draupnir/test/fixtures/configuration/literal.yaml |
|
28
|
+
$ draupnir get draupnir/test/fixtures/configuration/literal.yaml | treyja json
|
44
29
|
{"tensors":[{"dims":[3,2],"dataType":"FLOAT","int32Data":[],"int64Data":[],"uint32Data":[],"uint64Data":[],"floatData":[0.2,0.30000001,1,2,3,5],"doubleData":[]}]}
|
45
30
|
{"tensors":[{"dims":[3,2],"dataType":"FLOAT","int32Data":[],"int64Data":[],"uint32Data":[],"uint64Data":[],"floatData":[0,0,0.5,0.60000002,0.69999999,0.80000001],"doubleData":[]}]}
|
46
31
|
```
|
@@ -50,7 +35,7 @@ $ draupnir get draupnir/test/fixtures/configuration/literal.yaml | $TREYJA_RUBY_
|
|
50
35
|
[jq](https://stedolan.github.io/jq/) command is handy to pretty print JSON.
|
51
36
|
|
52
37
|
```console
|
53
|
-
$ draupnir get draupnir/test/fixtures/configuration/literal.yaml |
|
38
|
+
$ draupnir get draupnir/test/fixtures/configuration/literal.yaml | treyja json | jq
|
54
39
|
{
|
55
40
|
"tensors": [
|
56
41
|
{
|
@@ -111,10 +96,10 @@ Shape of input tensors must be 3 dimensional and channel count must be 1 (graysc
|
|
111
96
|
Element type of input tensors must be uint8, int8, float or double.
|
112
97
|
|
113
98
|
```console
|
114
|
-
$ draupnir get mnist --take 100 |
|
99
|
+
$ draupnir get mnist --take 100 | treyja image --output tmp/mnist-images
|
115
100
|
```
|
116
101
|
|
117
|
-

|
118
103
|

|
119
104
|

|
120
105
|

|
data/exe/treyja
CHANGED
@@ -1,211 +1,5 @@
|
|
1
1
|
#! /usr/bin/env ruby
|
2
2
|
|
3
|
-
|
4
|
-
require 'tensors_pb.rb'
|
3
|
+
require "treyja/exe"
|
5
4
|
|
6
|
-
|
7
|
-
'h' => 'help',
|
8
|
-
'help' => 'help',
|
9
|
-
'output' => 'output',
|
10
|
-
'normalize' => 'normalize',
|
11
|
-
'round' => 'round'
|
12
|
-
}
|
13
|
-
|
14
|
-
$options = [
|
15
|
-
*ARGV.map { |k| (ms = k.match(/^--?(\w+)/)) ? [$unabbrev[ms[1]], true] : nil },
|
16
|
-
*ARGV.each_cons(2).map { |k, v| (v !~ /^-/ && ms = k.match(/^--?(\w+)/)) ? [$unabbrev[ms[1]], v] : nil }
|
17
|
-
].compact.to_h
|
18
|
-
|
19
|
-
$command, $file = ARGV.take_while { |s| s !~ /^-/ }
|
20
|
-
if $command.nil? || $options["help"]
|
21
|
-
puts <<EOS
|
22
|
-
Usage: treyja COMMAND [FILE] [OPTIONS]
|
23
|
-
|
24
|
-
If FILE is not given, it reads from STDIN.
|
25
|
-
|
26
|
-
Available options:
|
27
|
-
-h,--help Show this help text
|
28
|
-
--output DIR Directory to output images
|
29
|
-
--normalize Normalize floating values (image only)
|
30
|
-
--round INT Digit to round floating values (default: 4)
|
31
|
-
|
32
|
-
Available commands:
|
33
|
-
json Output tensors in JSON format
|
34
|
-
csv Output tensors in CSV format
|
35
|
-
image Create png images and output to DIR
|
36
|
-
dump Dump in the format of inner expression
|
37
|
-
EOS
|
38
|
-
exit
|
39
|
-
end
|
40
|
-
|
41
|
-
$round = $options.fetch('round', 4).to_f
|
42
|
-
if $round > 0
|
43
|
-
class Float
|
44
|
-
def to_s_with_round
|
45
|
-
self.round($round).to_s_without_round
|
46
|
-
end
|
47
|
-
alias_method :to_s_without_round, :to_s
|
48
|
-
alias_method :to_s, :to_s_with_round
|
49
|
-
end
|
50
|
-
|
51
|
-
class Double
|
52
|
-
def to_s_with_round
|
53
|
-
self.round($round).to_s_without_round
|
54
|
-
end
|
55
|
-
alias_method :to_s_without_round, :to_s
|
56
|
-
alias_method :to_s, :to_s_with_round
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def reader
|
61
|
-
io = $file ? open($file) : STDIN
|
62
|
-
io.binmode
|
63
|
-
|
64
|
-
Enumerator.new do |y|
|
65
|
-
while magic = io.read(4)
|
66
|
-
raise "Incorrect magic bytes" unless magic == 'XCIX'
|
67
|
-
length = io.read(8).reverse.unpack("Q").first.to_i
|
68
|
-
y << Tensors::TensorsProto.decode(io.read(length))
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
|
74
|
-
def write_image tensor, k, file
|
75
|
-
width, height, channel = tensor.dims
|
76
|
-
width ||= 1
|
77
|
-
height ||= 1
|
78
|
-
channel ||= 1
|
79
|
-
offset = width * height * channel * k
|
80
|
-
range = offset ... (width * height * channel + offset)
|
81
|
-
|
82
|
-
normalizer =
|
83
|
-
if $options['normalize']
|
84
|
-
-> (xs) {
|
85
|
-
min, max = xs.minmax
|
86
|
-
xs.map { |f| ((f - min) / (max - min) * 255).to_i }
|
87
|
-
}
|
88
|
-
else
|
89
|
-
-> (xs) { xs.map { |f| (f * 255).to_i } }
|
90
|
-
end
|
91
|
-
|
92
|
-
depth, org_data =
|
93
|
-
case tensor.data_type
|
94
|
-
when :UINT8
|
95
|
-
[8, tensor.byte_data.unpack('C*')[range]]
|
96
|
-
when :INT8
|
97
|
-
[8, tensor.byte_data.unpack('C*')[range]]
|
98
|
-
when :FLOAT
|
99
|
-
[8, normalizer.call(tensor.float_data[range])]
|
100
|
-
when :DOUBLE
|
101
|
-
[8, normalizer.call(tensor.double_data[range])]
|
102
|
-
else
|
103
|
-
raise "unsupported data type: #{tensor.data_type}"
|
104
|
-
end
|
105
|
-
|
106
|
-
color_type =
|
107
|
-
case channel
|
108
|
-
when 1
|
109
|
-
0 # grayscale
|
110
|
-
when 2
|
111
|
-
4 # grayscale and alpha
|
112
|
-
when 3
|
113
|
-
2 # rgb
|
114
|
-
when 4
|
115
|
-
6 # rgba
|
116
|
-
else
|
117
|
-
raise "unsupported channel: #{channel}"
|
118
|
-
end
|
119
|
-
|
120
|
-
raw_data = (0...height).map do |y|
|
121
|
-
(0...width).map do |x|
|
122
|
-
(0...channel).map do |ch|
|
123
|
-
org_data[ch * height * width + y * width + x]
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
def chunk(type, data)
|
129
|
-
[data.bytesize, type, data, Zlib.crc32(type + data)].pack("NA4A*N")
|
130
|
-
end
|
131
|
-
|
132
|
-
open(file, 'w') do |io|
|
133
|
-
io.print "\x89PNG\r\n\x1a\n"
|
134
|
-
io.print chunk("IHDR", [width, height, depth, color_type, 0, 0, 0].pack("NNCCCCC"))
|
135
|
-
img_data = raw_data.map {|line| ([0] + line.flatten).pack("C*") }.join
|
136
|
-
io.print chunk("IDAT", Zlib::Deflate.deflate(img_data))
|
137
|
-
io.print chunk("IEND", "")
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
def dims_to_indices dims
|
142
|
-
if dims.empty?
|
143
|
-
[[]]
|
144
|
-
else
|
145
|
-
x = dims.first
|
146
|
-
xs = dims[1..-1]
|
147
|
-
Enumerator.new do |y|
|
148
|
-
dims_to_indices(xs).each do |is|
|
149
|
-
x.times.each do |i|
|
150
|
-
y << [i, *is]
|
151
|
-
end
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
def raw_data tensor
|
158
|
-
case tensor.data_type
|
159
|
-
when :INT8, :UINT8
|
160
|
-
tensor.byte_data.unpack('C*')
|
161
|
-
when :FLOAT
|
162
|
-
tensor.float_data
|
163
|
-
when :DOUBLE
|
164
|
-
tensor.double_data
|
165
|
-
else
|
166
|
-
raise "unsupported data type: #{tensor.data_type}"
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
case $command
|
171
|
-
when "dump"
|
172
|
-
reader.each do |ts|
|
173
|
-
p ts
|
174
|
-
STDOUT.flush
|
175
|
-
end
|
176
|
-
when "json"
|
177
|
-
reader.each do |ts|
|
178
|
-
puts ts.to_json
|
179
|
-
STDOUT.flush
|
180
|
-
end
|
181
|
-
when "image"
|
182
|
-
dir = $options["output"]
|
183
|
-
raise "--output option required" unless dir
|
184
|
-
|
185
|
-
FileUtils.mkdir_p dir
|
186
|
-
|
187
|
-
require "zlib"
|
188
|
-
reader.each_with_index do |tensors, i|
|
189
|
-
tensors.tensors.each_with_index do |tensor, j|
|
190
|
-
n = tensor.dims.drop(3).inject(1, :*) # Drop width, height and channel and fold the rest
|
191
|
-
n.times.each do |k|
|
192
|
-
postfix = ([i, j] + (n > 1 ? [k] : [])).map(&:to_s).join('-')
|
193
|
-
dst = File.join(dir, "image-#{postfix}.png")
|
194
|
-
write_image tensor, k, dst
|
195
|
-
end
|
196
|
-
end
|
197
|
-
end
|
198
|
-
when "csv"
|
199
|
-
if head = reader.first
|
200
|
-
headers = head.tensors.each_with_index.flat_map do |t, i|
|
201
|
-
dims_to_indices(t.dims).map do |ix|
|
202
|
-
[i.to_s, ix.empty? ? nil : ix.reverse.map(&:to_s).join('_')].compact.join('-')
|
203
|
-
end
|
204
|
-
end
|
205
|
-
puts headers.join(',')
|
206
|
-
puts head.tensors.flat_map { |t| raw_data(t) }.map(&:to_s).join(',')
|
207
|
-
reader.each do |ts|
|
208
|
-
puts ts.tensors.flat_map { |t| raw_data(t) }.map(&:to_s).join(',')
|
209
|
-
end
|
210
|
-
end
|
211
|
-
end
|
5
|
+
Treyja::Exe.new(ARGV).run
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Treyja
|
2
|
+
module Command
|
3
|
+
class Csv
|
4
|
+
attr_reader :reader
|
5
|
+
|
6
|
+
def initialize reader
|
7
|
+
@reader = reader
|
8
|
+
end
|
9
|
+
|
10
|
+
def run
|
11
|
+
if head = reader.first
|
12
|
+
headers = head.tensors.each_with_index.flat_map do |t, i|
|
13
|
+
dims_to_indices(t.dims).map do |ix|
|
14
|
+
[i.to_s, ix.empty? ? nil : ix.reverse.map(&:to_s).join('_')].compact.join('-')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
puts headers.join(',')
|
18
|
+
puts head.tensors.flat_map { |t| raw_data(t) }.map(&:to_s).join(',')
|
19
|
+
reader.each do |ts|
|
20
|
+
puts ts.tensors.flat_map { |t| raw_data(t) }.map(&:to_s).join(',')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def dims_to_indices dims
|
28
|
+
if dims.empty?
|
29
|
+
[[]]
|
30
|
+
else
|
31
|
+
x = dims.first
|
32
|
+
xs = dims[1..-1]
|
33
|
+
Enumerator.new do |y|
|
34
|
+
dims_to_indices(xs).each do |is|
|
35
|
+
x.times.each do |i|
|
36
|
+
y << [i, *is]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def raw_data tensor
|
44
|
+
case tensor.data_type
|
45
|
+
when :INT8, :UINT8
|
46
|
+
tensor.byte_data.unpack('C*')
|
47
|
+
when :FLOAT
|
48
|
+
tensor.float_data
|
49
|
+
when :DOUBLE
|
50
|
+
tensor.double_data
|
51
|
+
else
|
52
|
+
raise "unsupported data type: #{tensor.data_type}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Treyja
|
2
|
+
module Command
|
3
|
+
class Help
|
4
|
+
def run
|
5
|
+
puts <<EOS
|
6
|
+
Usage: treyja COMMAND [FILE] [OPTIONS]
|
7
|
+
|
8
|
+
If FILE is not given, it reads from STDIN.
|
9
|
+
|
10
|
+
Available options:
|
11
|
+
-h,--help Show this help text
|
12
|
+
--output DIR Directory to output images
|
13
|
+
--normalize Normalize floating values (image only)
|
14
|
+
--round INT Digit to round floating values (default: 4)
|
15
|
+
|
16
|
+
Available commands:
|
17
|
+
json Output tensors in JSON format
|
18
|
+
csv Output tensors in CSV format
|
19
|
+
image Create png images and output to DIR
|
20
|
+
dump Dump in the format of inner expression
|
21
|
+
EOS
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require "zlib"
|
2
|
+
|
3
|
+
module Treyja
|
4
|
+
module Command
|
5
|
+
class Image
|
6
|
+
attr_reader :reader, :output_dir, :options
|
7
|
+
|
8
|
+
def initialize reader, output_dir, options = {}
|
9
|
+
@reader = reader
|
10
|
+
@output_dir = output_dir
|
11
|
+
@options = options
|
12
|
+
end
|
13
|
+
|
14
|
+
def run
|
15
|
+
FileUtils.mkdir_p output_dir
|
16
|
+
|
17
|
+
reader.each_with_index do |tensors, i|
|
18
|
+
tensors.tensors.each_with_index do |tensor, j|
|
19
|
+
n = tensor.dims.drop(3).inject(1, :*) # Drop width, height and channel and fold the rest
|
20
|
+
n.times.each do |k|
|
21
|
+
postfix = ([i, j] + (n > 1 ? [k] : [])).map(&:to_s).join('-')
|
22
|
+
dst = File.join(output_dir, "image-#{postfix}.png")
|
23
|
+
write_image tensor, k, dst
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
reader.each do |ts|
|
28
|
+
p ts
|
29
|
+
STDOUT.flush
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def write_image tensor, k, file
|
36
|
+
width, height, channel = tensor.dims
|
37
|
+
width ||= 1
|
38
|
+
height ||= 1
|
39
|
+
channel ||= 1
|
40
|
+
offset = width * height * channel * k
|
41
|
+
range = offset ... (width * height * channel + offset)
|
42
|
+
|
43
|
+
normalizer =
|
44
|
+
if options['normalize']
|
45
|
+
-> (xs) {
|
46
|
+
min, max = xs.minmax
|
47
|
+
xs.map { |f| ((f - min) / (max - min) * 255).to_i }
|
48
|
+
}
|
49
|
+
else
|
50
|
+
-> (xs) { xs.map { |f| (f * 255).to_i } }
|
51
|
+
end
|
52
|
+
|
53
|
+
depth, org_data =
|
54
|
+
case tensor.data_type
|
55
|
+
when :UINT8
|
56
|
+
[8, tensor.byte_data.unpack('C*')[range]]
|
57
|
+
when :INT8
|
58
|
+
[8, tensor.byte_data.unpack('C*')[range]]
|
59
|
+
when :FLOAT
|
60
|
+
[8, normalizer.call(tensor.float_data[range])]
|
61
|
+
when :DOUBLE
|
62
|
+
[8, normalizer.call(tensor.double_data[range])]
|
63
|
+
else
|
64
|
+
raise "unsupported data type: #{tensor.data_type}"
|
65
|
+
end
|
66
|
+
|
67
|
+
color_type =
|
68
|
+
case channel
|
69
|
+
when 1
|
70
|
+
0 # grayscale
|
71
|
+
when 2
|
72
|
+
4 # grayscale and alpha
|
73
|
+
when 3
|
74
|
+
2 # rgb
|
75
|
+
when 4
|
76
|
+
6 # rgba
|
77
|
+
else
|
78
|
+
raise "unsupported channel: #{channel}"
|
79
|
+
end
|
80
|
+
|
81
|
+
raw_data = (0...height).map do |y|
|
82
|
+
(0...width).map do |x|
|
83
|
+
(0...channel).map do |ch|
|
84
|
+
org_data[ch * height * width + y * width + x]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def chunk(type, data)
|
90
|
+
[data.bytesize, type, data, Zlib.crc32(type + data)].pack("NA4A*N")
|
91
|
+
end
|
92
|
+
|
93
|
+
open(file, 'w') do |io|
|
94
|
+
io.print "\x89PNG\r\n\x1a\n"
|
95
|
+
io.print chunk("IHDR", [width, height, depth, color_type, 0, 0, 0].pack("NNCCCCC"))
|
96
|
+
img_data = raw_data.map {|line| ([0] + line.flatten).pack("C*") }.join
|
97
|
+
io.print chunk("IDAT", Zlib::Deflate.deflate(img_data))
|
98
|
+
io.print chunk("IEND", "")
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
data/lib/treyja/exe.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require "optparse"
|
2
|
+
|
3
|
+
require "treyja/reader"
|
4
|
+
require "treyja/command/csv"
|
5
|
+
require "treyja/command/dump"
|
6
|
+
require "treyja/command/help"
|
7
|
+
require "treyja/command/image"
|
8
|
+
require "treyja/command/json"
|
9
|
+
|
10
|
+
class Float
|
11
|
+
def self.round_digits
|
12
|
+
@round_digits
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s_with_round
|
16
|
+
Float.round_digits ?
|
17
|
+
self.round(Float.round_digits).to_s_without_round :
|
18
|
+
self.to_s_without_round
|
19
|
+
end
|
20
|
+
|
21
|
+
alias_method :to_s_without_round, :to_s
|
22
|
+
alias_method :to_s, :to_s_with_round
|
23
|
+
end
|
24
|
+
|
25
|
+
module Treyja
|
26
|
+
class Exe
|
27
|
+
def initialize args
|
28
|
+
@args = args
|
29
|
+
end
|
30
|
+
|
31
|
+
def options
|
32
|
+
@options ||= {}
|
33
|
+
end
|
34
|
+
|
35
|
+
def patch_round round
|
36
|
+
if round > 0
|
37
|
+
::Float.instance_variable_set "@round_digits", round
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def run
|
42
|
+
opts = OptionParser.new
|
43
|
+
opts.on("--output DIR")
|
44
|
+
opts.on("--normalize")
|
45
|
+
opts.on("--round INT")
|
46
|
+
opts.on("--version") do
|
47
|
+
puts "Version: #{Treyja::VERSION}"
|
48
|
+
return
|
49
|
+
end
|
50
|
+
opts.on("-h", "--help") do
|
51
|
+
Treyja::Command::Help.new.run
|
52
|
+
return
|
53
|
+
end
|
54
|
+
command, file = opts.parse!(@args, into: options)
|
55
|
+
|
56
|
+
patch_round options.fetch(:round, 4).to_f
|
57
|
+
case command
|
58
|
+
when "dump"
|
59
|
+
reader = Treyja::Reader.new file
|
60
|
+
Treyja::Command::Dump.new(reader).run
|
61
|
+
when "json"
|
62
|
+
reader = Treyja::Reader.new file
|
63
|
+
Treyja::Command::Json.new(reader).run
|
64
|
+
when "image"
|
65
|
+
output_dir = options[:output]
|
66
|
+
raise "--output option required" unless output_dir
|
67
|
+
|
68
|
+
reader = Treyja::Reader.new file
|
69
|
+
Treyja::Command::Image.new(reader, output_dir, options.slice(:normalize)).run
|
70
|
+
when "csv"
|
71
|
+
reader = Treyja::Reader.new file
|
72
|
+
Treyja::Command::Csv.new(reader).run
|
73
|
+
when nil
|
74
|
+
Treyja::Command::Help.new.run
|
75
|
+
else
|
76
|
+
puts "Unknown command: #{command}"
|
77
|
+
Treyja::Command::Help.new.run
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
File without changes
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "treyja/proto/tensors_pb"
|
2
|
+
|
3
|
+
module Treyja
|
4
|
+
class Reader
|
5
|
+
extend Forwardable
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
delegate [:each] => :@enumerator
|
9
|
+
|
10
|
+
MAGIC_BYTES = "XCIX"
|
11
|
+
|
12
|
+
def initialize file = nil
|
13
|
+
io = file ? open(file) : STDIN
|
14
|
+
io.binmode
|
15
|
+
|
16
|
+
@enumerator = Enumerator.new do |y|
|
17
|
+
while magic = io.read(4)
|
18
|
+
raise "Incorrect magic bytes" unless magic == MAGIC_BYTES
|
19
|
+
length = io.read(8).reverse.unpack("Q").first.to_i
|
20
|
+
y << Tensors::TensorsProto.decode(io.read(length))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/treyja/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: treyja
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cross Compass
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-05-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: google-protobuf
|
@@ -102,12 +102,18 @@ files:
|
|
102
102
|
- default.nix
|
103
103
|
- exe/treyja
|
104
104
|
- gemset.nix
|
105
|
-
- lib/tensor_pb.rb
|
106
|
-
- lib/tensors_pb.rb
|
107
105
|
- lib/treyja.rb
|
106
|
+
- lib/treyja/command/csv.rb
|
107
|
+
- lib/treyja/command/dump.rb
|
108
|
+
- lib/treyja/command/help.rb
|
109
|
+
- lib/treyja/command/image.rb
|
110
|
+
- lib/treyja/command/json.rb
|
111
|
+
- lib/treyja/exe.rb
|
112
|
+
- lib/treyja/proto/tensor_pb.rb
|
113
|
+
- lib/treyja/proto/tensors_pb.rb
|
114
|
+
- lib/treyja/reader.rb
|
108
115
|
- lib/treyja/version.rb
|
109
116
|
- treyja.gemspec
|
110
|
-
- treyja/.gitignore
|
111
117
|
homepage: http://github.com/xc-jp/treyja-ruby
|
112
118
|
licenses:
|
113
119
|
- MIT
|