data-science

Data science, data analysis, and machine learning in Clojure https://scicloj.github.io/pages/chat_streams/ for additional discussions
2019-11-30T01:25:11.173Z

Hot off the press from AWS - deep learning library for Java

πŸŽ‰ 2
2019-11-30T01:25:13.173200Z

https://djl.ai/

2019-11-30T01:25:46.174Z

Haven’t looked at it deeply yet - but looks like another avenue for us to leverage

2019-11-30T01:27:50.174700Z

From the contributor list - a lot of MXNet AWS folks involved

2019-11-30T01:51:33.175Z

It looks like this might be the official announcement: https://towardsdatascience.com/introducing-deep-java-library-djl-9de98de8c6ca

viesti 2019-11-30T10:15:27.175600Z

;; deps.edn
{:deps {org.apache.logging.log4j/log4j-slf4j-impl {:mvn/version "2.12.1"}
        ai.djl.mxnet/mxnet-model-zoo {:mvn/version "0.2.0"}
        ai.djl.mxnet/mxnet-native-mkl$osx-x86_64 {:mvn/version "1.6.0-a"}}}

;; example
(ns clj-djl.core
  (:require [<http://clojure.java.io|clojure.java.io> :as io])
  (:import (ai.djl.modality.cv.util BufferedImageUtils)
           (ai.djl.mxnet.zoo MxModelZoo)
           (ai.djl.training.util ProgressBar)
           (ai.djl.modality.cv ImageVisualization)
           (javax.imageio ImageIO)))

(defn example []
  (let [img (BufferedImageUtils/fromUrl "<https://raw.githubusercontent.com/dmlc/web-data/master/gluoncv/pose/soccer.png>")]
    (with-open [model (.loadModel (MxModelZoo/SSD) (ProgressBar.))]
      (let [predict-result (-> model
                               (.newPredictor)
                               (.predict img))]
        (ImageVisualization/drawBoundingBoxes img predict-result)
        (ImageIO/write img "png" (io/file "ssd.png"))))))

viesti 2019-11-30T10:15:46.175800Z

I got a picture πŸ™‚

viesti 2019-11-30T10:48:58.176200Z

the Java API interops nicely

viesti 2019-11-30T10:49:30.176600Z

learned how to do maven classifier in deps.edn too πŸ™‚

viesti 2019-11-30T10:53:36.177300Z

forgot to close Predictor in above, seems it implements AutoCloseable

viesti 2019-11-30T11:15:12.178600Z

not meaning to start a wrapper, but put the above example into a repo: https://github.com/viesti/clj-djl/blob/master/src/clj_djl/core.clj

viesti 2019-11-30T11:16:15.179900Z

the Java API might not need a wrapper at all, since interop works nicely

2019-11-30T13:04:33.181500Z

Great! Thanks for blazing the way in trying it out :) I plan to check it out too in the near future

2019-11-30T14:10:41.183100Z

It seems great. (Wished it was more functional though) xD

viesti 2019-11-30T19:13:39.183800Z

More tinkering (was looking at MXNet tutorials: https://mxnet.apache.org/api/python/docs/tutorials/getting-started/crash-course/1-ndarray.html):

clj-djl.core&gt; (do (import (ai.djl.ndarray NDManager))
                  (import (ai.djl.ndarray.types Shape))
                  (with-open [nd-manager (NDManager/newBaseManager)]
                    (println (.randomUniform nd-manager 1 -1 (Shape. [2 2])))))
#object[ai.djl.mxnet.engine.MxNDArray 0x7b62d17b ND: (2, 2) cpu(0) float32
[[ 0.1527, -0.2471],
 [-0.2918,  0.2312],
]
]

πŸ‘ 1
viesti 2019-11-30T19:16:19.184800Z

seems that djl has a JNA wrapper around MXNet

viesti 2019-11-30T19:26:21.185500Z

which is quite interesting, they have a tool to generate JNA mappings from the MXNet C header file: https://github.com/awslabs/djl/tree/master/mxnet/jnarator

viesti 2019-11-30T19:27:20.186300Z

I wonder how this compares to the hand-written JNI in the Scala/Java bindgins of MXNet: https://github.com/apache/incubator-mxnet/blob/master/scala-package/native/src/main/native/org_apache_mxnet_native_c_api.cc

viesti 2019-11-30T19:28:53.187400Z

this generator tool reminds me of SWIG (http://www.swig.org/), although that generates straight JNI from C headers

viesti 2019-11-30T19:30:02.188Z

adopting new features from MXNet might be faster via a generator

2019-11-30T21:30:48.188800Z

interesting - @chris441 brought up JNA for TVM / MXNet a bit ago https://discuss.tvm.ai/t/clojure-bindings-for-tvm/1127

viesti 2019-12-01T12:02:29.193900Z

This πŸ˜„ > We would also like to see more java integrations to DMLC projects that allow easier access to multiple languages; ones that force unnecessary dependencies on the scala compiler also force dependencies on the root jvm runtime (mxnet, we are looking at you).

2019-11-30T21:36:03.190Z

The low level mappings are great - but there is a lot of other work built on the higher inference / training / dataset stuff too

2019-11-30T21:36:14.190300Z

At least in the main MXNet library

2019-11-30T21:36:57.191100Z

That said - it definitely is a nice accessible point of integration

2019-11-30T21:37:28.191400Z

definitely could be useful for new stuff

2019-11-30T21:40:21.192700Z

Please feel free to keep experimenting and pursue any good avenues. I don’t have a lot of time just now to dive in. Probably won’t have any until after the holidays πŸ™‚