Skip to content

Instantly share code, notes, and snippets.

@ankane
Created September 16, 2019 19:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ankane/3bb4ddbf84edd7f05a24cd3697ccd9a7 to your computer and use it in GitHub Desktop.
Save ankane/3bb4ddbf84edd7f05a24cd3697ccd9a7 to your computer and use it in GitHub Desktop.
# https://github.com/onnx/models/tree/master/vision/body_analysis/emotion_ferplus
require "onnxruntime"
require "mini_magick"
img = MiniMagick::Image.open("ranger.jpg")
img.crop "100x100+60+20", "-gravity", "center"
img.resize "64x64^", "-gravity", "center", "-extent", "64x64"
img.colorspace "Gray"
img.write("resized.jpg")
# all pixels are the same for grayscale, so just get one of them
pixels = img.get_pixels.flat_map { |r| r.map(&:first) }
pixels = OnnxRuntime::Utils.reshape(pixels, [1, 1, 64, 64])
model = OnnxRuntime::Model.new("model.onnx")
result = model.predict("Input3" => pixels)
def softmax(x)
exp = x.map { |v| Math.exp(v - x.max) }
exp.map { |v| v / exp.sum }
end
probabilities = softmax(result["Plus692_Output_0"].first)
emotion_labels = [
"neutral", "happiness", "surprise", "sadness",
"anger", "disgust", "fear", "contempt"
]
pp emotion_labels.zip(probabilities).sort_by { |_, v| -v }.to_h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment