Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ Now we can run the inference.

```ruby
# execute inference
inferenced_results = model.run image_set
inference_results = model.run image_set
```

The `inferenced_results` is the array that contains the hash of results of `output_layers`. So you can get each value as follows.
The `inference_results` is the array that contains the hash of results of `output_layers`. So you can get each value as follows.

```ruby
fc6_out = inferenced_results.find { |x| x[:name] == FC6_OUT_NAME }
softmax_out = inferenced_results.find { |x| x[:name] == SOFTMAX_OUT_NAME }
fc6_out = inference_results.find { |x| x[:name] == FC6_OUT_NAME }
softmax_out = inference_results.find { |x| x[:name] == SOFTMAX_OUT_NAME }
```

That's it.
Expand Down
4 changes: 2 additions & 2 deletions example/example_mnist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@
}
]
# execute inference
inferenced_results = model.run image_set
inference_results = model.run image_set

categories = (0..9).to_a
TOP_K = 1
layer_result = inferenced_results.find { |x| x[:name] == MNIST_OUT_NAME }
layer_result = inference_results.find { |x| x[:name] == MNIST_OUT_NAME }
layer_result[:data].zip(image_list).each do |image_result, image_filepath|
# sort by score
sorted_result = image_result.zip(categories).sort_by { |x| -x[0] }
Expand Down
4 changes: 2 additions & 2 deletions example/example_mnist_with_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@
}
]
# execute inference
model.run image_set do |inferenced_results|
model.run image_set do |inference_results|
categories = (0..9).to_a
TOP_K = 1
layer_result = inferenced_results.find { |x| x[:name] == MNIST_OUT_NAME }
layer_result = inference_results.find { |x| x[:name] == MNIST_OUT_NAME }
layer_result[:data].zip(image_list).each do |image_result, image_filepath|
# sort by score
sorted_result = image_result.zip(categories).sort_by { |x| -x[0] }
Expand Down
4 changes: 2 additions & 2 deletions example/example_vgg16.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ def download_file(url, output)
]

# execute inference
inferenced_results = model.run image_set
inference_results = model.run image_set

# load category definition
categories = File.read('./data/synset_words.txt').split("\n")
TOP_K = 5
layer_result = inferenced_results.find { |x| x[:name] == SOFTMAX_OUT_NAME }
layer_result = inference_results.find { |x| x[:name] == SOFTMAX_OUT_NAME }
layer_result[:data].zip(image_list).each do |image_result, image_filepath|
puts "=== Result for #{image_filepath} ==="

Expand Down
60 changes: 28 additions & 32 deletions lib/menoh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ class MenohModel
int8: Numo::Int8,
int16: Numo::Int16,
int32: Numo::Int32,
int64: Numo::Int64,
}
int64: Numo::Int64
}.freeze

def initialize(menoh, option)
if option[:input_layers].nil? || option[:input_layers].empty?
raise "Required ':input_layers'"
end
raise "Required ':input_layers'" unless option[:input_layers].instance_of?(Array)

option[:input_layers].each_with_index do |input_layer, i|
raise 'Invalid option : input_layers' unless input_layer.instance_of?(Hash)
raise "Invalid name for input_layer[#{i}]" unless input_layer[:name].instance_of?(String)
Expand All @@ -46,7 +47,7 @@ def initialize(menoh, option)
end

option = option.dup
if option.has_key?(:backend_config)
if option.key?(:backend_config)
config = option[:backend_config]
unless config.nil? || config.is_a?(String)
option[:backend_config] = JSON.dump(config)
Expand All @@ -58,50 +59,45 @@ def initialize(menoh, option)
yield self if block_given?
end

def run(dataset)
def run(dataset, numo_narray: false)
raise 'Invalid dataset' if !dataset.instance_of?(Array) || dataset.empty?
if dataset.length != @option[:input_layers].length
raise "Invalid input num: expected==#{@option[:input_layers].length} actual==#{dataset.length}"
end

dataset.each do |input|
if !input[:data].instance_of?(Array) || input[:data].empty?
raise "Empty data for layer #{input[:name]}" if input[:data].empty?

if input[:data].instance_of?(Array)
set_data(input[:name], input[:data])
elsif input[:data].instance_of?(Numo::SFloat)
if dataset.length != @option[:input_layers].length
raise "Invalid input num: expected==#{@option[:input_layers].length} actual==#{dataset.length}"
end
set_data_str(input[:name], input[:data].to_binary)
else
raise "Invalid dataset for layer #{input[:name]}"
end
set_data(input[:name], input[:data])
end

# run
native_run

# reshape result
results = @option[:output_layers].map do |name|
buffer = get_data(name)
dtype = get_dtype(name)
shape = get_shape(name)
{ name: name, shape: shape, data: Util.reshape(buffer, shape) }
end

yield results if block_given?
results
end

def run_numo(dataset)
raise 'Invalid dataset' if !dataset.instance_of?(Array) || dataset.empty?
if dataset.length != @option[:input_layers].length
raise "Invalid input num: expected==#{@option[:input_layers].length} actual==#{dataset.length}"
end
dataset.each do |input|
set_data_str(input[:name], input[:data].to_binary)
end
data = nil
if numo_narray
c = DTYPE_TO_NUMO_NARRAY_CLASS[dtype]
raise InvalidDType, "unsupported dtype: #{dtype}" if c.nil?

# run
native_run

results = {}
@option[:output_layers].each do |name|
dtype = get_dtype(name)
c = DTYPE_TO_NUMO_NARRAY_CLASS[dtype]
raise InvalidDTypeError.new("unsupported dtype: #{dtype}") if c.nil?
results[name] = c.from_binary(get_data_str(name), get_shape(name))
data = c.from_binary(get_data_str(name), get_shape(name))
else
buffer = get_data(name)
data = Util.reshape(buffer, shape)
end
{ name: name, shape: shape, data: data }
end

yield results if block_given?
Expand Down
66 changes: 39 additions & 27 deletions test/menoh_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,59 @@ def test_menoh_basic_function
}
model = onnx.make_model(model_opt)
assert_instance_of(Menoh::MenohModel, model)
# input: Array, output: Array
10.times do
imageset = [
{
name: MNIST_IN_NAME,
data: (0..(batch_size - 1)).map { |_i| (0..(1 * 28 * 28 - 1)).to_a }.flatten
}
]
inferenced_results = model.run imageset
assert_instance_of(Array, inferenced_results)
assert_equal(MNIST_OUT_NAME, inferenced_results.first[:name])
assert_equal(batch_size, inferenced_results.first[:data].length)
inference_results = model.run imageset
assert_instance_of(Array, inference_results)
assert_equal(MNIST_OUT_NAME, inference_results.first[:name])
assert_equal(batch_size, inference_results.first[:data].length)
end
end

def test_menoh_basic_function_numo
onnx = Menoh::Menoh.new(MNIST_ONNX_FILE)
assert_instance_of(Menoh::Menoh, onnx)
batch_size = 3
model_opt = {
backend: 'mkldnn',
input_layers: [
# input: Array, output: Numo::SFloat
10.times do
imageset = [
{
name: MNIST_IN_NAME,
dims: [batch_size, 1, 28, 28]
data: (0..(batch_size - 1)).map { |_i| (0..(1 * 28 * 28 - 1)).to_a }.flatten
}
],
output_layers: [MNIST_OUT_NAME]
}
model = onnx.make_model(model_opt)
assert_instance_of(Menoh::MenohModel, model)
]
inference_results = model.run(imageset, numo_narray: true)
assert_instance_of(Array, inference_results)
assert_equal(MNIST_OUT_NAME, inference_results.first[:name])
assert_instance_of(Numo::SFloat, inference_results.first[:data])
assert_equal([batch_size, 10], inference_results.first[:data].shape)
end
# input: Numo::SFloat, output: Numo::SFloat
10.times do
imageset = [
{
name: MNIST_IN_NAME,
data: Numo::SFloat.zeros(batch_size, 1, 28, 28)
}
]
inference_results = model.run(imageset, numo_narray: true)
assert_instance_of(Array, inference_results)
assert_equal(MNIST_OUT_NAME, inference_results.first[:name])
assert_instance_of(Numo::SFloat, inference_results.first[:data])
assert_equal([batch_size, 10], inference_results.first[:data].shape)
end
# input: Numo::SFloat, output: Array
10.times do
imageset = [
{
name: MNIST_IN_NAME,
data: Numo::SFloat.zeros(batch_size, 1, 28, 28)
}
]
inferenced_results = model.run_numo imageset
assert_instance_of(Hash, inferenced_results)
assert_instance_of(Numo::SFloat, inferenced_results[MNIST_OUT_NAME])
assert_equal([batch_size, 10], inferenced_results[MNIST_OUT_NAME].shape)
inference_results = model.run(imageset)
assert_instance_of(Array, inference_results)
assert_equal(MNIST_OUT_NAME, inference_results.first[:name])
assert_equal(batch_size, inference_results.first[:data].length)
end
end

Expand All @@ -92,10 +104,10 @@ def test_menoh_basic_function_with_block
assert_instance_of(Menoh::Menoh, onnx)
onnx.make_model(model_opt) do |model|
assert_instance_of(Menoh::MenohModel, model)
model.run(imageset) do |inferenced_results|
assert_instance_of(Array, inferenced_results)
assert_equal(MNIST_OUT_NAME, inferenced_results.first[:name])
assert_equal(batch_size, inferenced_results.first[:data].length)
model.run(imageset) do |inference_results|
assert_instance_of(Array, inference_results)
assert_equal(MNIST_OUT_NAME, inference_results.first[:name])
assert_equal(batch_size, inference_results.first[:data].length)
end
end
end
Expand Down