Search in sources :

Example 6 with Session

use of org.tensorflow.Session in project sponge by softelnet.

the class TensorflowTest method testTf.

@Test
public void testTf() throws UnsupportedEncodingException {
    try (Graph g = new Graph()) {
        final String value = "Hello from " + TensorFlow.version();
        // named "MyConst" with a value "value".
        try (Tensor t = Tensor.create(value.getBytes("UTF-8"))) {
            // The Java API doesn't yet include convenience functions for adding operations.
            g.opBuilder("Const", "MyConst").setAttr("dtype", t.dataType()).setAttr("value", t).build();
        }
        // Execute the "MyConst" operation in a Session.
        try (Session s = new Session(g);
            Tensor output = s.runner().fetch("MyConst").run().get(0)) {
            logger.info(new String(output.bytesValue(), "UTF-8"));
            logger.info(output.toString());
        }
    }
}
Also used : Graph(org.tensorflow.Graph) Tensor(org.tensorflow.Tensor) Session(org.tensorflow.Session) Test(org.junit.Test)

Example 7 with Session

use of org.tensorflow.Session in project sponge by softelnet.

the class TensorflowTest method testLoadModel.

// TODO @Test
public void testLoadModel() throws Exception {
    String modelDir = "examples/tensorflow/estimator/model";
    SavedModelBundle bundle = SavedModelBundle.load(modelDir + "/" + SpongeUtils.getLastSubdirectory(modelDir), "serve");
    try (Session s = bundle.session()) /* ; Tensor output = s.runner().fetch("MyConst").run().get(0) */
    {
        Tensor x = Tensor.create(new float[] { 2, 5, 8, 1 });
        Tensor y = s.runner().feed("x", x).fetch("y").run().get(0);
        logger.info("y = {}", y.floatValue());
    }
}
Also used : Tensor(org.tensorflow.Tensor) SavedModelBundle(org.tensorflow.SavedModelBundle) Session(org.tensorflow.Session)

Example 8 with Session

use of org.tensorflow.Session in project tensorflow-java-yolo by szaza.

the class ObjectDetector method executeYOLOGraph.

/**
 * Executes graph on the given preprocessed image
 * @param image preprocessed image
 * @return output tensor returned by tensorFlow
 */
private float[] executeYOLOGraph(final Tensor<Float> image) {
    try (Graph graph = new Graph()) {
        graph.importGraphDef(GRAPH_DEF);
        try (Session s = new Session(graph);
            Tensor<Float> result = s.runner().feed("input", image).fetch("output").run().get(0).expect(Float.class)) {
            float[] outputTensor = new float[YOLOClassifier.getInstance().getOutputSizeByShape(result)];
            FloatBuffer floatBuffer = FloatBuffer.wrap(outputTensor);
            result.writeTo(floatBuffer);
            return outputTensor;
        }
    }
}
Also used : Graph(org.tensorflow.Graph) FloatBuffer(java.nio.FloatBuffer) Session(org.tensorflow.Session)

Example 9 with Session

use of org.tensorflow.Session in project zoltar by spotify.

the class TensorFlowExtrasTest method testExtract2a.

@Test
public void testExtract2a() {
    final Graph graph = createDummyGraph();
    final Session session = new Session(graph);
    final Session.Runner runner = session.runner();
    runner.feed("input", Tensors.create(10.0));
    final Map<String, JTensor> result = TensorFlowExtras.runAndExtract(runner, mul2, mul3);
    assertEquals(Lists.newArrayList(mul2, mul3), new ArrayList<>(result.keySet()));
    assertScalar(result.get(mul2), 20.0);
    assertScalar(result.get(mul3), 30.0);
    session.close();
    graph.close();
}
Also used : Graph(org.tensorflow.Graph) Session(org.tensorflow.Session) Test(org.junit.Test)

Example 10 with Session

use of org.tensorflow.Session in project zoltar by spotify.

the class TensorFlowExtrasTest method testExtract2b.

@Test
public void testExtract2b() {
    final Graph graph = createDummyGraph();
    final Session session = new Session(graph);
    final Session.Runner runner = session.runner();
    runner.feed("input", Tensors.create(10.0));
    final Map<String, JTensor> result = TensorFlowExtras.runAndExtract(runner, mul3, mul2);
    assertEquals(Lists.newArrayList(mul3, mul2), new ArrayList<>(result.keySet()));
    assertScalar(result.get(mul2), 20.0);
    assertScalar(result.get(mul3), 30.0);
    session.close();
    graph.close();
}
Also used : Graph(org.tensorflow.Graph) Session(org.tensorflow.Session) Test(org.junit.Test)

Aggregations

Session (org.tensorflow.Session)13 Graph (org.tensorflow.Graph)9 Test (org.junit.Test)6 Tensor (org.tensorflow.Tensor)5 PredictorsTest (com.spotify.zoltar.PredictorsTest)2 GraphBuilder (edu.ml.tensorflow.util.GraphBuilder)2 FloatBuffer (java.nio.FloatBuffer)2 Path (java.nio.file.Path)2 LongBuffer (java.nio.LongBuffer)1 SavedModelBundle (org.tensorflow.SavedModelBundle)1