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());
}
}
}
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());
}
}
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;
}
}
}
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();
}
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();
}
Aggregations