Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Hanns Holger Rutz
    @Sciss
    Ok, thanks. I'll check out both.
    Anthony Platanios
    @eaplatanios
    Hi! I’m behind tf-scala referenced above. It’s true that I haven’t updated the library in a while because I don’t have much free time, but it does work with TF 2.4 currently and we are also using it in production, so it’s reasonably stable.
    Karl Lessard
    @karllessard
    On that @eaplatanios , do you think it could be interesting to « merge » tf-scala in tf-java so you won’t have to maintain a separate project by yourself? Are you aware of any important feature currently missing in tf-java, but present in tf-scala, that would enhance user experience for Scala developers?
    Hanns Holger Rutz
    @Sciss
    Thank you, it's good to hear that.
    Hanns Holger Rutz
    @Sciss

    HI there. Are there any self-contained example projects to look at? I'm trying this, using sbt 1.5.5:

    lazy val deps = new {
      val tensorflow = "0.3.3"
    }
    
    lazy val root = project.in(file("."))
      .settings(
        scalaVersion := "3.0.2",
        libraryDependencies ++= Seq(
          "org.tensorflow" % "tensorflow-core-api"  % deps.tensorflow,
          "org.tensorflow" % "tensorflow-core-api"  % deps.tensorflow classifier "linux-x86_64-mkl",
          "org.tensorflow" % "tensorflow-framework" % deps.tensorflow,
        )
      )

    and

    package de.sciss.tf
    
    import org.tensorflow.EagerSession
    
    object Test:
      def main(args: Array[String]): Unit =
        val input   = Array(-10.0, -5.0, 0.0, 5.0, 10.0)
        val options = EagerSession.options()
        val session = EagerSession.initDefault(options)
        println("Here")
        session.close()

    But running it just tells me something for JNI isn't working yet:

    Warning: Could not load Loader: java.lang.UnsatisfiedLinkError: no jnijavacpp in java.library.path: [/usr/java/packages/lib, /usr/lib/x86_64-linux-gnu/jni, /lib/x86_64-linux-gnu, /usr/lib/x86_64-linux-gnu, /usr/lib/jni, /lib, /usr/lib]
    Warning: Could not load Pointer: java.lang.UnsatisfiedLinkError: no jnijavacpp in java.library.path: [/usr/java/packages/lib, /usr/lib/x86_64-linux-gnu/jni, /lib/x86_64-linux-gnu, /usr/lib/x86_64-linux-gnu, /usr/lib/jni, /lib, /usr/lib]
    Warning: Could not load BytePointer: java.lang.UnsatisfiedLinkError: no jnijavacpp in java.library.path: [/usr/java/packages/lib, /usr/lib/x86_64-linux-gnu/jni, /lib/x86_64-linux-gnu, /usr/lib/x86_64-linux-gnu, /usr/lib/jni, /lib, /usr/lib]
    2021-10-03 16:07:13.749139: I external/org_tensorflow/tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
    To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
    2021-10-03 16:07:13.749982: I external/org_tensorflow/tensorflow/core/common_runtime/process_util.cc:146] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
    Here
    Exception in thread "main" java.lang.IllegalStateException: Default eager session cannot be closed
        at org.tensorflow.EagerSession.close(EagerSession.java:261)
        at de.sciss.tf.Test$.main(Test.scala:11)
        at de.sciss.tf.Test.main(Test.scala)
    Like a Java example showing simple cycle of training an ANN, then creating some predictions.
    Hanns Holger Rutz
    @Sciss
    Trying to work through this tutorial https://www.baeldung.com/tensorflow-java - but Tensor.create seems to no longer exist. How do you create a constant scalar in the current version?
    and there is a more type safe API than stringly typed opBuilder?
    Hanns Holger Rutz
    @Sciss
    Ok, getting the hang of it. Is this the idiomatic way?
    val g       = new Graph()
    val ops     = Ops.create(g)
    import ops.*
    val a       = constant(3.0)
    val b       = constant(2.0)
    val x       = placeholder(classOf[TFloat64])
    val y       = placeholder(classOf[TFloat64])
    val ax      = math.mul(a, x)
    val by      = math.mul(b, y)
    val z       = math.add(ax, by)
    val session = new Session(g)
    val r       = session.runner()
    val tensor  = r.fetch(z)
      .feed(x, TFloat64.scalarOf(3.0))
      .feed(y, TFloat64.scalarOf(6.0))
      .run
      .get(0)
    
    tensor match
      case tf: TFloat64 => println(tf.getDouble())  // 21.0
      case other        => println(s"OTHER: $other")
    Adam Pocock
    @Craigacp
    The examples repo is here https://github.com/TensorFlow/java-models
    That tutorial is for the old 1
    1.x API. The tests in the TensorFlow Java repo are the most up to date way of doing things. Note at the moment there's an issue with gradient determinism when building models for training that causes incorrect outputs occasionally.
    We are working on a higher level API, but it's not all available yet
    Hanns Holger Rutz
    @Sciss
    thanks. I think with a bit of Scala facade, this already looks quite nice and concise.
    Hanns Holger Rutz
    @Sciss
    Is it possible to use keras API, e.g. https://www.tensorflow.org/tutorials/generative/dcgan ? or do I have to translate that back to plain TF ?
    Adam Pocock
    @Craigacp
    We're working on the higher level API, which will be Keras like, but the model and layer apis are not integrated yet.
    Hanns Holger Rutz
    @Sciss
    Ok, I'm trying to update an older keras wrapper project, it uses the old Java API. What this the new way of writing Tensors.create(float[][])?
    Hanns Holger Rutz
    @Sciss
    I can create 1D vector with TFloat32.vectorOf, but 2D?
    Tensor.of(TFloat32.class, Shape.of(javaArray.length, javaArray[0].length), data -> StdArrays.copyTo(javaArray, data)) I guess ... ? Is there a utility function that does this?
    Hanns Holger Rutz
    @Sciss

    I'm stuck with these:

    public class Ones {
        public <T extends TNumber> Operand<T> initialize(Ops tf, Operand<TInt32> shape, Class<T> dtype) {
            return tf.fill(shape, tf.constant(1.0f, dtype));   // no such method
        }
    }

    How can I convert from a float to a TNumber? In Scala this would be a type class instance TNumbers[T <: TNumber] { def fromFloat(f: Float): Operand[T] }, but I don't know how to express that in Java. Should I change Class<T> for something that has factory capabilities?

    Adam Pocock
    @Craigacp
    Off the top of my head you could use tf.cast.
    Karl Lessard
    @karllessard

    To create a 2D tensor from a 2D java array, you can invoke TFloat32.tensorOf(StdArrays.ndCopyOf(javaArray)).

    If you are looking to create a constant though, there is a shortcut function that allows you to do simply tf.constant(javaArray)

    If you can avoid completely the use of standard Java arrays and only rely on the NdArray hierarchy from the start, you’ll have better performances and memory usage
    Hanns Holger Rutz
    @Sciss
    I see, thanks. For now, I'm just trying to make somebody else's library compile ;)
    This does compile indeed tf.fill(shape, tf.dtypes.cast(tf.constant(1.0f), dtype)). So it won't fail at runtime?
    raftaa
    @raftaa
    Hi, I have a question concerning saving/exporting/loading a model for deployment. I think I want something like a frozen graph of my model i.e. one single pb file. I used tf.io.write_graph in python to write my model into a pb file. However when trying to load it in Java via SavedModelLoader.loader(pb_file_directory) I get an exception: "Could not find meta graph def matching supplied tags: { serve }. To inspect available tag-sets in the SavedModel, please use the SavedModel CLI: saved_model_cli". And saved_model_cli tells me that "SavedModel file does not exist" in that directory. I'm confused and my question is: how is this usually done and what works for TF java?
    Adam Pocock
    @Craigacp
    If you've got a frozen graph you can load that directly by reading it into a GraphDef and supplying that to the Graph before making a session. That's not how TF 2 (or even later versions of TF 1) do deployment, they use SavedModels which is a folder containing a graph pb and a checkpoint pb of the variables.
    TF-Java uses SavedModelBundle to load in saved models.
    raftaa
    @raftaa
    I understand. Thanks Adam!
    Hanns Holger Rutz
    @Sciss

    Trying to translate this tutorial. Python:

    noise = tf.random.normal([1, 100])

    How do I write that in Java/Scala? I try

    val noise = tf.random.randomStandardNormal(Shape.of(1, 100), classOf[TFloat32])

    But this is not an Operand[? <: TNumber]. How I get from Shape to that?

    Is tf.constant(Array(1, 100)) correct?
    raftaa
    @raftaa
    Hi there. I've a question again: I want to use mask_rcnn_inception_resnet_v2_1024x1024_coco17_gpu-8 from the TF2 model zoo for semantic image segmantation. I trained the CNN with python tools and now I'm trying to get the masks in our JVM inference implementation. I wonder if I can use the implementation for the "normal" FasterRCNN resulting to BBoxes. Are the masks available in the outputTensorMap? There is a key "mask_prediction" with a Tensor of shape [100, 4, 33, 33] which looks promising. Am I on the right track? How is it thought to access the masks in the java world?
    Adam Pocock
    @Craigacp
    TF-Java will return tensors the same way the model would in Python, so it should have an equivalent output with equivalent meaning.
    Jakob Sultan Ericsson
    @jakeri
    I saw that you merged 2.7 to master. Will there be a release soon?
    Karl Lessard
    @karllessard
    We are experimenting some issues with the GPU build on Linux, as soon as this is fixed and the tests are conclusive, we’ll release it
    f
    @SidneyLann
    java-tf still has no automatic derivation?
    Karl Lessard
    @karllessard
    You mean automatic differentiation? Yes there is, check at the optimizers in tensorflow-framework, is it what you are looking for?
    Karl Lessard
    @karllessard
    But if you mean gradient tape (in eager mode), this is still lacking, as the TF core lib does not expose yet what we need to support it
    f
    @SidneyLann
    What's the different? I want to do automatic BP.
    Karl Lessard
    @karllessard
    If you build a graph and want to train with automatic BP, the optimizers I’ve pointed at will do the job for you, check here for an example.
    Gradient tapes are used to calculate loss in eager mode (i.e. without graphs). If you use a graph, you are good to go.
    f
    @SidneyLann
    Ok. Thanks.
    Peter
    @phydesmith
    Hello!
    Is it ok to ask questions regarding the TF java api here?
    Karl Lessard
    @karllessard
    of course!
    Peter
    @phydesmith
    Hi Karl, thanks - I know you've helped me on my issue regarding converting buffered images to tensors on github, so I'm glad you're here. I have been working on it again in the last few weeks and have a few follow on questions.
    I'm not sure if you remember, but you posted some code that basically did some pre-processing on the buffered image bytes before putting them into the Tensor.
    Peter
    @phydesmith
    yes, exactly that