Skip to content

Instantly share code, notes, and snippets.

@ankane
Last active May 10, 2023 17:58
Show Gist options
  • Select an option

  • Save ankane/4a9681c8d9b9e814debe9e3ea836529d to your computer and use it in GitHub Desktop.

Select an option

Save ankane/4a9681c8d9b9e814debe9e3ea836529d to your computer and use it in GitHub Desktop.
require "onnxruntime"
require "mini_magick"
img = MiniMagick::Image.open("bears.jpg")
pixels = img.get_pixels
model = OnnxRuntime::Model.new("model.onnx")
result = model.predict({"inputs" => [pixels]})
p result["num_detections"]
p result["detection_classes"]
coco_labels = {
23 => "bear",
88 => "teddy bear"
}
def draw_box(img, label, box)
width, height = img.dimensions
thickness = 2
top = (box[0] * height).round - thickness
left = (box[1] * width).round - thickness
bottom = (box[2] * height).round + thickness
right = (box[3] * width).round + thickness
# draw box
img.combine_options do |c|
c.draw "rectangle #{left},#{top} #{right},#{bottom}"
c.fill "none"
c.stroke "red"
c.strokewidth thickness
end
# draw text
img.combine_options do |c|
c.draw "text #{left},#{top - 5} \"#{label}\""
c.fill "red"
c.pointsize 18
end
end
result["num_detections"].each_with_index do |n, idx|
n.to_i.times do |i|
label = result["detection_classes"][idx][i].to_i
label = coco_labels[label] || label
box = result["detection_boxes"][idx][i]
draw_box(img, label, box)
end
end
img.write("labeled.jpg")

ghost commented Oct 16, 2021

Copy link
Copy Markdown

This code snippet isn't working properly as of October 2021. It keeps throwing this error:

in 'predict' : wrong number of arguments (given 0, expected 1) (ArgumentError)

@ankane

ankane commented Oct 16, 2021

Copy link
Copy Markdown
Author

Hey @Ashvith, thanks for reporting! I've updated the gist and post for Ruby 3 and tf2onnx 1.9.

ghost commented Oct 16, 2021

Copy link
Copy Markdown

Hey @ankane , I was wondering if this Python code is unneeded, and is it possible to convert the model through Ruby?

python -m tf2onnx.convert --opset 10 --fold_const \
  --saved-model ssd_mobilenet_v1_coco_2018_01_28/saved_model \
  --output model.onnx

@ankane

ankane commented Oct 16, 2021

Copy link
Copy Markdown
Author

I'm not aware of any Ruby libraries that can convert TensorFlow models to ONNX.

@Anuj4444

Anuj4444 commented Dec 7, 2021

Copy link
Copy Markdown

Hello @ankane and @Ashvith

I am trying to run this code and getting the following error while running "result = model.predict({"inputs" => [pixels]})"

`block in create_input_tensor': Unknown input: inputs (OnnxRuntime::Error)

Please let me know what am I doing wrong.

@ankane

ankane commented Jul 5, 2022

Copy link
Copy Markdown
Author

Make sure you're using the latest version of tf2onnx to convert the model. Also, the blog post has info on how to check the input names.

@TommasoMascioli

Copy link
Copy Markdown

great work

@rgaufman

Copy link
Copy Markdown

Where can I find tf2onnx.convert to create model.onnx?

@ankane

ankane commented May 10, 2023

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment