Search in sources :

Example 6 with Graph

use of org.tensorflow.Graph 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 Graph

use of org.tensorflow.Graph 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 8 with Graph

use of org.tensorflow.Graph 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 9 with Graph

use of org.tensorflow.Graph 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)

Example 10 with Graph

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

the class TensorFlowExtrasTest method testExtract1.

@Test
public void testExtract1() {
    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);
    assertEquals(Sets.newHashSet(mul2), result.keySet());
    assertScalar(result.get(mul2), 20.0);
    session.close();
    graph.close();
}
Also used : Graph(org.tensorflow.Graph) Session(org.tensorflow.Session) Test(org.junit.Test)

Aggregations

Graph (org.tensorflow.Graph)11 Session (org.tensorflow.Session)9 Test (org.junit.Test)4 GraphBuilder (edu.ml.tensorflow.util.GraphBuilder)2 FloatBuffer (java.nio.FloatBuffer)2 Path (java.nio.file.Path)1 Tensor (org.tensorflow.Tensor)1