menoh 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +55 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Dockerfile +49 -0
- data/Gemfile +5 -0
- data/LICENSE +21 -0
- data/README.md +80 -0
- data/Rakefile +18 -0
- data/Vagrantfile +46 -0
- data/bin/console +10 -0
- data/bin/setup +6 -0
- data/docs/tutorial.md +121 -0
- data/example/data/0.png +0 -0
- data/example/data/1.png +0 -0
- data/example/data/2.png +0 -0
- data/example/data/3.png +0 -0
- data/example/data/4.png +0 -0
- data/example/data/5.png +0 -0
- data/example/data/6.png +0 -0
- data/example/data/7.png +0 -0
- data/example/data/8.png +0 -0
- data/example/data/9.png +0 -0
- data/example/data/mnist.onnx +0 -0
- data/example/example_mnist.rb +74 -0
- data/example/example_mnist_with_block.rb +75 -0
- data/example/example_vgg16.rb +89 -0
- data/ext/menoh_native/extconf.rb +14 -0
- data/ext/menoh_native/menoh_ruby.c +328 -0
- data/ext/menoh_native/menoh_ruby.h +7 -0
- data/lib/menoh.rb +87 -0
- data/lib/menoh/version.rb +3 -0
- data/menoh.gemspec +29 -0
- metadata +146 -0
data/lib/menoh.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'menoh/version'
|
2
|
+
require 'menoh/menoh_native'
|
3
|
+
|
4
|
+
module Menoh
|
5
|
+
class Menoh
|
6
|
+
def initialize(file)
|
7
|
+
if !File.exist?(file)
|
8
|
+
raise "No such file : #{file}"
|
9
|
+
end
|
10
|
+
|
11
|
+
native_init file
|
12
|
+
yield self if block_given?
|
13
|
+
end
|
14
|
+
|
15
|
+
def make_model(option)
|
16
|
+
if option[:backend].nil?
|
17
|
+
raise "Required ':backend' : #{option[:backend]}"
|
18
|
+
end
|
19
|
+
model = MenohModel.new self, option
|
20
|
+
yield model if block_given?
|
21
|
+
model
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def transpose(buffer, shape)
|
27
|
+
sliced_buffer = buffer.each_slice(buffer.length / shape[0]).to_a
|
28
|
+
if shape.length > 2
|
29
|
+
next_shape = shape.slice(1, a.length)
|
30
|
+
sliced_buffer = sliced_buffer.map { |buf| transpose buf, next_shape }
|
31
|
+
end
|
32
|
+
sliced_buffer
|
33
|
+
end
|
34
|
+
|
35
|
+
module Menoh
|
36
|
+
class MenohModel
|
37
|
+
def initialize(menoh, option)
|
38
|
+
if option[:input_layers].nil? || option[:input_layers].empty?
|
39
|
+
raise "Required ':input_layers'"
|
40
|
+
end
|
41
|
+
raise "Required ':input_layers'" unless option[:input_layers].instance_of?(Array)
|
42
|
+
option[:input_layers].each_with_index do |input_layer, i|
|
43
|
+
raise 'Invalid option : input_layers' unless input_layer.instance_of?(Hash)
|
44
|
+
raise "Invalid name for input_layer[#{i}]" unless input_layer[:name].instance_of?(String)
|
45
|
+
raise "Invalid dims for input_layer[#{i}]" unless input_layer[:dims].instance_of?(Array)
|
46
|
+
end
|
47
|
+
if option[:output_layers].nil? || option[:output_layers].empty?
|
48
|
+
raise "Invalid ':output_layers'"
|
49
|
+
end
|
50
|
+
native_init menoh, option
|
51
|
+
@option = option
|
52
|
+
yield self if block_given?
|
53
|
+
end
|
54
|
+
|
55
|
+
def run(dataset)
|
56
|
+
raise 'Invalid dataset' if !dataset.instance_of?(Array) || dataset.empty?
|
57
|
+
if dataset.length != @option[:input_layers].length
|
58
|
+
raise "Invalid input num: expected==#{@option[:input_layers].length} actual==#{dataset.length}"
|
59
|
+
end
|
60
|
+
dataset_for_native = []
|
61
|
+
dataset.each do |input|
|
62
|
+
if !input[:data].instance_of?(Array) || input[:data].empty?
|
63
|
+
raise "Invalid dataset for layer #{input[:name]}"
|
64
|
+
end
|
65
|
+
target_layer = @option[:input_layers].find { |item| item[:name] == input[:name] }
|
66
|
+
expected_data_length = target_layer[:dims].inject(:*)
|
67
|
+
if input[:data].length != expected_data_length
|
68
|
+
raise "Invalid data length: expected==#{expected_data_length} actual==#{input[:data].length}"
|
69
|
+
end
|
70
|
+
dataset_for_native << input[:data]
|
71
|
+
end
|
72
|
+
|
73
|
+
# run
|
74
|
+
results = native_run dataset_for_native
|
75
|
+
|
76
|
+
# reshape result
|
77
|
+
results.map do |raw|
|
78
|
+
buffer = raw[:data]
|
79
|
+
shape = raw[:shape]
|
80
|
+
raw[:data] = transpose buffer, shape
|
81
|
+
end
|
82
|
+
|
83
|
+
yield results if block_given?
|
84
|
+
results
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
data/menoh.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'menoh/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'menoh'
|
7
|
+
spec.version = Menoh::VERSION
|
8
|
+
spec.authors = ['Kunihiko MIYOSHI']
|
9
|
+
spec.email = ['miyoshik@preferred.jp']
|
10
|
+
|
11
|
+
spec.summary = "Ruby binding of ONNX runtime engine 'Menoh'"
|
12
|
+
spec.description = "Ruby binding of ONNX runtime engine 'Menoh'"
|
13
|
+
spec.homepage = 'https://github.com/pfnet-research/menoh-ruby'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = 'exe'
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
spec.extensions = ['ext/menoh_native/extconf.rb']
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
25
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
26
|
+
spec.add_development_dependency 'pry'
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
28
|
+
spec.add_development_dependency 'rake-compiler'
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: menoh
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kunihiko MIYOSHI
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-07-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake-compiler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Ruby binding of ONNX runtime engine 'Menoh'
|
84
|
+
email:
|
85
|
+
- miyoshik@preferred.jp
|
86
|
+
executables: []
|
87
|
+
extensions:
|
88
|
+
- ext/menoh_native/extconf.rb
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- CODE_OF_CONDUCT.md
|
93
|
+
- Dockerfile
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- Vagrantfile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- docs/tutorial.md
|
102
|
+
- example/data/0.png
|
103
|
+
- example/data/1.png
|
104
|
+
- example/data/2.png
|
105
|
+
- example/data/3.png
|
106
|
+
- example/data/4.png
|
107
|
+
- example/data/5.png
|
108
|
+
- example/data/6.png
|
109
|
+
- example/data/7.png
|
110
|
+
- example/data/8.png
|
111
|
+
- example/data/9.png
|
112
|
+
- example/data/mnist.onnx
|
113
|
+
- example/example_mnist.rb
|
114
|
+
- example/example_mnist_with_block.rb
|
115
|
+
- example/example_vgg16.rb
|
116
|
+
- ext/menoh_native/extconf.rb
|
117
|
+
- ext/menoh_native/menoh_ruby.c
|
118
|
+
- ext/menoh_native/menoh_ruby.h
|
119
|
+
- lib/menoh.rb
|
120
|
+
- lib/menoh/version.rb
|
121
|
+
- menoh.gemspec
|
122
|
+
homepage: https://github.com/pfnet-research/menoh-ruby
|
123
|
+
licenses:
|
124
|
+
- MIT
|
125
|
+
metadata: {}
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 2.5.2.1
|
143
|
+
signing_key:
|
144
|
+
specification_version: 4
|
145
|
+
summary: Ruby binding of ONNX runtime engine 'Menoh'
|
146
|
+
test_files: []
|