menoh 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ffe56775dcbde74f64fdeed4fa59bf6fcc539803
4
+ data.tar.gz: cc676fe5e5d2f2a2e591125fc6dcdfc9c4054962
5
+ SHA512:
6
+ metadata.gz: 6b1fb84c56bf469d76f97c86e0a0b0b0a832f146ff3ca52224d4983c429720c6dcb3c0191ca87fc1fb925234ea2d04bda2960a280265e52b7d790e270d73df4d
7
+ data.tar.gz: 62f09585697c3d536d55fef32f0477b289cb0593786e97ae7820759b87fc749d72446d8434abf23550c626e5737fc5e999c4375a4fa46807c31d85ddeeea3b14
@@ -0,0 +1,55 @@
1
+ *.gem
2
+ *.rbc
3
+ /.vagrant/
4
+ /.config
5
+ /coverage/
6
+ /external/
7
+ /InstalledFiles
8
+ /pkg/
9
+ /spec/reports/
10
+ /spec/examples.txt
11
+ /test/tmp/
12
+ /test/version_tmp/
13
+ /tmp/
14
+
15
+ # Used by dotenv library to load environment variables.
16
+ # .env
17
+
18
+ ## Specific to RubyMotion:
19
+ .dat*
20
+ .repl_history
21
+ build/
22
+ *.bridgesupport
23
+ build-iPhoneOS/
24
+ build-iPhoneSimulator/
25
+
26
+ ## Specific to RubyMotion (use of CocoaPods):
27
+ #
28
+ # We recommend against adding the Pods directory to your .gitignore. However
29
+ # you should judge for yourself, the pros and cons are mentioned at:
30
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
31
+ #
32
+ # vendor/Pods/
33
+
34
+ ## Documentation cache and generated files:
35
+ /.yardoc/
36
+ /_yardoc/
37
+ /doc/
38
+ /rdoc/
39
+
40
+ ## Environment normalization:
41
+ /.bundle/
42
+ /vendor/bundle
43
+ /lib/bundler/man/
44
+
45
+ # for a library or gem, you might want to ignore these files since the code is
46
+ # intended to run in multiple environments; otherwise, check them in:
47
+ # Gemfile.lock
48
+ # .ruby-version
49
+ # .ruby-gemset
50
+
51
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
52
+ .rvmrc
53
+
54
+ *.so
55
+ *.log
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at miyoshik@preferred.jp. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
@@ -0,0 +1,49 @@
1
+ FROM ubuntu:xenial-20180228
2
+
3
+ LABEL maintainer "Kunihiko Miyoshi <miyoshik@preferred.jp>"
4
+ LABEL OBJECT="Menoh Ruby Extension Reference Environment"
5
+
6
+ ENV INSTALL_PREFIX /usr/local
7
+ ENV LD_LIBRARY_PATH ${INSTALL_PREFIX}/lib
8
+
9
+ RUN apt-get update && apt-get install -y \
10
+ git \
11
+ gcc \
12
+ g++ \
13
+ cmake \
14
+ cmake-data \
15
+ libopencv-dev \
16
+ protobuf-compiler \
17
+ libprotobuf-dev \
18
+ ruby-dev \
19
+ ruby-rmagick \
20
+ ruby-bundler && \
21
+ rm -rf /var/lib/apt/lists/*
22
+
23
+ # MKL-DNN
24
+ RUN mkdir /opt/mkl-dnn
25
+ WORKDIR /opt/mkl-dnn
26
+ RUN git clone https://github.com/01org/mkl-dnn.git && \
27
+ cd mkl-dnn/scripts && bash ./prepare_mkl.sh && cd .. && \
28
+ sed -i 's/add_subdirectory(examples)//g' CMakeLists.txt && \
29
+ sed -i 's/add_subdirectory(tests)//g' CMakeLists.txt && \
30
+ mkdir -p build && cd build && cmake -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX .. && make && \
31
+ make install
32
+
33
+ # Menoh
34
+ WORKDIR /opt/
35
+ RUN git clone https://github.com/pfnet-research/menoh.git && \
36
+ cd menoh && \
37
+ sed -i 's/add_subdirectory(example)//g' CMakeLists.txt && \
38
+ sed -i 's/add_subdirectory(test)//g' CMakeLists.txt && \
39
+ mkdir build && \
40
+ cd build && \
41
+ cmake -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX .. && \
42
+ make install
43
+
44
+ # menoh-ruby
45
+ RUN gem install rake-compiler
46
+ RUN mkdir /opt/menoh-ruby
47
+ ADD . /opt/menoh-ruby
48
+ WORKDIR /opt/menoh-ruby
49
+ RUN rake && rake install
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Preferred Networks, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,80 @@
1
+ # Menoh Ruby Extension
2
+
3
+ This is a Ruby extension of [Menoh](https://github.com/pfnet-research/menoh); an ONNX runtime engine developed by [@okdshin](https://github.com/okdshin) and their team [@pfnet-research](https://github.com/pfnet-research).
4
+
5
+ ## Installation
6
+
7
+ You need `ruby-dev`, `bundler`, `rake-compiler` to install this extension.
8
+
9
+ ```bash
10
+ $ sudo apt install ruby-dev
11
+ $ sudo gem install bundler
12
+ $ sudo gem install rake-compiler
13
+ ```
14
+
15
+ And add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'menoh'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install menoh
28
+
29
+ ## Usage
30
+
31
+ Please see [Menoh tutorial](https://github.com/pfnet-research/menoh/blob/master/docs/tutorial.md) and [menoh-ruby tutorial](https://github.com/pfnet-research/menoh-ruby/blob/master/docs/tutorial.md).
32
+ And we have [some examples](https://github.com/pfnet-research/menoh-ruby/blob/master/example/) on this repository.
33
+
34
+ ## Development
35
+
36
+ After checking out the repo, run `bin/setup` to install ruby dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
37
+
38
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
39
+
40
+ ### Docker
41
+
42
+ You can develop on docker. For details, please refer to [Dockerfile](Dockerfile).
43
+
44
+ ```bash
45
+ $ export IMAGE_VERSION=0.0.0 # please specify version
46
+ $ sudo -E docker build -t menoh-ruby:$IMAGE_VERSION `pwd`
47
+ $ sudo -E docker run -it --name menoh-ruby-test -v $(pwd):/opt/menoh-ruby --entrypoint /bin/bash menoh-ruby:$IMAGE_VERSION
48
+ $ cd /opt/menoh-ruby
49
+ $ rake && rake install
50
+
51
+ ```
52
+
53
+ #### attach after stop
54
+
55
+ ```bash
56
+ sudo docker start menoh-ruby-test bash
57
+ sudo docker attach menoh-ruby-test
58
+ ```
59
+
60
+ ### Vagrant
61
+
62
+ You can also set up the development environment by using Vagrant. The details are available on [Vagrantfile](Vagrantfile).
63
+
64
+ ```bash
65
+ $ vagrant up
66
+ $ vagrant ssh
67
+ $ cd /vagrant
68
+ ```
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pfnet-research/menoh-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
77
+
78
+ ## Code of Conduct
79
+
80
+ Everyone interacting in the OnnxInstant project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/pfnet-research/menoh-ruby/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,18 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ require 'rake/extensiontask'
11
+
12
+ task build: :compile
13
+
14
+ Rake::ExtensionTask.new('menoh_native') do |ext|
15
+ ext.lib_dir = 'lib/menoh'
16
+ end
17
+
18
+ task default: %i[clobber compile test]
@@ -0,0 +1,46 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ Vagrant.configure('2') do |config|
5
+ config.vm.box = 'ubuntu/xenial64'
6
+
7
+ config.vm.provider 'virtualbox' do |vb|
8
+ vb.memory = '4096'
9
+ vb.cpus = 2
10
+ end
11
+
12
+ config.vm.provision 'shell', inline: <<-SHELL
13
+ export INSTALL_PREFIX=/usr/local
14
+ sudo apt update
15
+ sudo apt install -y gcc g++ cmake cmake-data libopencv-dev
16
+ sudo apt install -y ruby-dev ruby-rmagick
17
+ sudo gem install bundler rake-compiler
18
+
19
+ # protobuf
20
+ sudo apt install -y protobuf-compiler libprotobuf-dev
21
+
22
+ # mkl-dnn
23
+ cd
24
+ git clone https://github.com/01org/mkl-dnn.git
25
+ cd mkl-dnn/scripts && bash ./prepare_mkl.sh && cd ..
26
+ sed -i 's/add_subdirectory(examples)//g' CMakeLists.txt
27
+ sed -i 's/add_subdirectory(tests)//g' CMakeLists.txt
28
+ mkdir -p build && cd build && cmake -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX .. && make
29
+ sudo make install
30
+
31
+ # Menoh
32
+ cd
33
+ git clone https://github.com/pfnet-research/menoh.git
34
+ cd menoh
35
+ sed -i 's/add_subdirectory(example)//g' CMakeLists.txt
36
+ sed -i 's/add_subdirectory(test)//g' CMakeLists.txt
37
+ mkdir -p build && cd build && cmake -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX .. && make
38
+ sudo make install
39
+
40
+ # menoh-ruby
41
+ export LD_LIBRARY_PATH=${INSTALL_PREFIX}/lib:$LD_LIBRARY_PATH
42
+ cd /vagrant
43
+ rake
44
+ sudo rake install
45
+ SHELL
46
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "menoh"
5
+
6
+ require "pry"
7
+ Pry.start
8
+
9
+ require "irb"
10
+ IRB.start(__FILE__)
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,121 @@
1
+ # Tutorial
2
+
3
+ In this tutorial, we are going to make a CNN model inference software.
4
+ This tutorial is based on [Menoh](https://github.com/pfnet-research/menoh)'s [original tutorial](https://github.com/pfnet-research/menoh/blob/master/docs/tutorial.md).
5
+
6
+ This script loads `data/VGG16.onnx` and takes input image, then outputs classification result.
7
+
8
+ ## Setup model
9
+
10
+ For gettinig ONNX model's named variables, please refer to [Menoh Tutorial](https://github.com/pfnet-research/menoh/blob/master/docs/tutorial.md).
11
+
12
+ VGG16 has one input and one output. So now we can check that the input name is *140326425860192* (input of 0:Conv) and the output name is *140326200803680* (output of 39:Softmax).
13
+
14
+ Some of we are interested the feature vector of input image. So in addition, we are going to take the output of 32:FC(fc6, which is the first FC layer after CNNs) named *140326200777584*.
15
+
16
+ We define name aliases for convenience:
17
+
18
+ ```ruby
19
+ CONV1_1_IN_NAME = '140326425860192'.freeze
20
+ FC6_OUT_NAME = '140326200777584'.freeze
21
+ SOFTMAX_OUT_NAME = '140326200803680'.freeze
22
+ ```
23
+
24
+ To build model, we load model data from ONNX file:
25
+
26
+ ```ruby
27
+ onnx_obj = Menoh::Menoh.new './data/VGG16.onnx'
28
+ ```
29
+
30
+ Now let's build the model.
31
+
32
+ ```ruby
33
+ # data shape of input images
34
+ input_shape = {
35
+ channel_num: 3,
36
+ width: 224,
37
+ height: 224
38
+ }
39
+ # model options for model
40
+ model_opt = {
41
+ backend: 'mkldnn',
42
+ input_layers: [
43
+ {
44
+ name: CONV1_1_IN_NAME,
45
+ dims: [
46
+ image_list.length,
47
+ input_shape[:channel_num],
48
+ input_shape[:width],
49
+ input_shape[:height]
50
+ ]
51
+ }
52
+ ],
53
+ output_layers: [FC6_OUT_NAME, SOFTMAX_OUT_NAME]
54
+ }
55
+ # make model for inference under 'model_opt'
56
+ model = onnx_obj.make_model model_opt
57
+ ```
58
+
59
+ ## Preprocessing dataset
60
+
61
+ Before running the inference, the preprocessing of input dataset is required. `data/VGG16.onnx` takes 3 channels 224 x 224 sized image but input image is not always sized 224x224. So we use Imagemagick's `resize_to_fill` method for resizing.
62
+
63
+ `VGG16.onnx`'s input layer *140326425860192* takes images as NCHW format (N x Channels x Height x Width). But RMagick's image array has alternately flatten values for each channel. So next we call `export_pixels` method for each channels `['B', 'G', 'R']`, then `flatten` arrays.
64
+
65
+ ```ruby
66
+ image_list = [
67
+ './data/Light_sussex_hen.jpg',
68
+ './data/honda_nsx.jpg',
69
+ ]
70
+ image_set = [
71
+ {
72
+ name: CONV1_1_IN_NAME,
73
+ data: image_list.map do |image_filepath|
74
+ image = Magick::Image.read(image_filepath).first
75
+ image = image.resize_to_fill(input_shape[:width], input_shape[:height])
76
+ 'BGR'.split('').map do |color|
77
+ image.export_pixels(0, 0, image.columns, image.rows, color).map { |pix| pix / 256 }
78
+ end.flatten
79
+ end.flatten
80
+ }
81
+ ]
82
+ ```
83
+
84
+ In current case, the range of pixel value `data/VGG16.onnx` taking is [0, 256]. On the other hand RMagick's image array takes [0, 65536]. So we have to scale the values by dividing 256.
85
+
86
+ And sometimes model takes values scaled in range [0, 1] or something. In that case, we can scale values here:
87
+
88
+ ```ruby
89
+ image_set = [
90
+ {
91
+ name: CONV1_1_IN_NAME,
92
+ data: image_list.map do |image_filepath|
93
+ image = Magick::Image.read(image_filepath).first
94
+ image = image.resize_to_fill(input_shape[:width], input_shape[:height])
95
+ 'BGR'.split('').map do |color|
96
+ image.export_pixels(0, 0, image.columns, image.rows, color).map { |pix| pix / 65536 }
97
+ end.flatten
98
+ end.flatten
99
+ }
100
+ ]
101
+ ```
102
+
103
+ ## Run inference and get results
104
+
105
+ Now we can run the inference.
106
+
107
+ ```ruby
108
+ # execute inference
109
+ inferenced_results = model.run image_set
110
+ ```
111
+
112
+ The `inferenced_results` is the array that contains the hash of results of `output_layers`. So you can get each value as follows.
113
+
114
+ ```ruby
115
+ fc6_out = inferenced_results.find { |x| x[:name] == FC6_OUT_NAME }
116
+ softmax_out = inferenced_results.find { |x| x[:name] == SOFTMAX_OUT_NAME }
117
+ ```
118
+
119
+ That's it.
120
+
121
+ The full code is available at [VGG16 example](https://github.com/pfnet-research/menoh-ruby/blob/master/example/example_vgg16.rb).