1

Tensorflow does not officially support Ruby. I was wondering how one can use tensorflow, full version (Python or JS), in a rails app.

While this is a general question, the answer may be of use to the community. No one has asked this and many people new to using other languages like Python in Rails would benefit.

Another way to state this question is what some of the successful companies using rails are doing to use tensorflow.

thebot
  • 249
  • 1
  • 2
  • 10
  • There is the non-official gem [`tensorflow.rb`](https://github.com/somaticio/tensorflow.rb). Doesn't seem to have updated in a few months, but it is most likely the best approach to using TensorFlow in Ruby. – jdehesa Jul 08 '19 at 15:49
  • @jdehesa Can one instead call python script from rails to run tensorflow? – thebot Jul 08 '19 at 16:16
  • Well, yes, you can always [call a Python script from Ruby](https://stackoverflow.com/q/18645352). Or you can try with something like [`pycall.rb`](https://github.com/mrkn/pycall.rb) - see [this issue](https://github.com/mrkn/pycall.rb/issues/67). Note this requires you to have an available Python environment to run your program. – jdehesa Jul 08 '19 at 16:29
  • @jdehesa Thank you. So would you say calling python from rails is a better -- perhaps currently the only -- way to use full version of tensorflow with rails? – thebot Jul 08 '19 at 17:20
  • From what I'm seeing of [`tensorflow.rb`](https://github.com/somaticio/tensorflow.rb), I think it might work with newer versions of the library. You just don't get the nicer functions from Python, and if you want to build a graph you have to do it at op level (e.g. see [`classify_image.rb`](https://github.com/somaticio/tensorflow.rb/blob/master/examples/image/classify_image.rb)). If you just want to load a saved model (see [`captcha.rb`](https://github.com/somaticio/tensorflow.rb/blob/master/examples/captcha/captcha.rb)) it should be easy. You may decide what suits you best. – jdehesa Jul 08 '19 at 17:26
  • Nowadays, if you're coming here via Google, it's probably best to look at https://github.com/ankane/tensorflow-ruby instead of tensorflow.rb. – Sixtyfive Jul 01 '23 at 12:56

1 Answers1

0

I'm only going by its readme, but the tensorflow ruby gem appears to be well supported.

This post shows how to use it to classify images

(I take directly from the article here)

Steps:

  • Clone and install tensorflow.rb
  • Go to image directory
  • Download this zip file from google
  • extract the file ‘tensorflow_inception_graph.pb’ in this directory.
  • Do ruby classify_image.rb (This will work on the default image of the Mysore palace).
  • To try this out on your own images you can add the image to this directory and then change file name in line 31 on classify_image.rb .
stevec
  • 41,291
  • 27
  • 223
  • 311