DEV Community

Cover image for Deep Learning Style Transformations in Ruby
kojix2
kojix2

Posted on

4

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)  β†©

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay