DEV Community

Cover image for Deep Learning Style Transformations in Ruby
kojix2
kojix2

Posted on

Deep Learning Style Transformations in Ruby

Want to do DeepLearning inference in Ruby?
Yes, you can!

Requirement

st.rb

require 'magro'
require 'onnxruntime'

model = OnnxRuntime::Model.new('')

input = Magro::IO.imread(ARGV[0])
                 .transpose(2, 0, 1)
                 .expand_dims(0)
                 .to_a

output = model.predict(input1: input)

result = Numo::Int32[*output['output1'][0]]
                    .clip(0, 255)
                    .transpose(1, 2, 0)
                    .cast_to(Numo::UInt8)

Magro::IO.imsave(ARGV[1], result)
Enter fullscreen mode Exit fullscreen mode

Run

Input image size should be a multiple of 4

ruby st.rb in.jpg out.jpg
Enter fullscreen mode Exit fullscreen mode

Generated images

https://commons.wikimedia.org/wiki/File:Arimatsushibori.JPG 1

Arimatsushibori

candy
candy.jpg

mosaic
mosaic.jpg

pointilism
pointilism

rain_princess
rain_princess.jpg

udnie
udnie.jpg

Beautiful.

Other posts


  1. Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)  

Top comments (0)