Java Code Examples for org.tensorflow.Graph#close()

The following examples show how to use org.tensorflow.Graph#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TensorFlowExtrasTest.java    From zoltar with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 2
Source File: TensorFlowExtrasTest.java    From zoltar with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 3
Source File: TensorFlowExtrasTest.java    From zoltar with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 4
Source File: RNTensorFlowGraphModule.java    From react-native-tensorflow with Apache License 2.0 5 votes vote down vote up
@ReactMethod
public void close(String id, Promise promise) {
    try {
        Graph graph = graphs.get(id);
        graph.close();
        promise.resolve(true);
    } catch (Exception e) {
        promise.reject(e);
    }
}
 
Example 5
Source File: TensorflowGraphUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenTensorflowGraphWhenRunInSessionReturnsExpectedResult() {
	
	Graph graph = TensorflowGraph.createGraph();
	Object result = TensorflowGraph.runGraph(graph, 3.0, 6.0);
	assertEquals(21.0, result);
	System.out.println(result);
	graph.close();
	
}
 
Example 6
Source File: InstSegMaskRCNN.java    From orbit-image-analysis with GNU General Public License v3.0 4 votes vote down vote up
public static void main2(String[] args) throws IOException {
    Date startDate = new Date();
    final InstSegMaskRCNN maskRCNN = new InstSegMaskRCNN();
    final byte[] graphDef = Files.readAllBytes(Paths.get(MODEL_DIR, MODEL_NAME));
    final Graph g = maskRCNN.loadGraph(graphDef);
    final Session s = maskRCNN.createSession(g);
    try {
        BufferedImage originalImage = ImageIO.read(new File(INPUT_IMAGE));
        long startt = System.currentTimeMillis();
        List<Callable<BufferedImage>> tasks = new ArrayList<>();
        for (int i=0; i<100; i++) {
            tasks.add(new Callable<BufferedImage>() {
                @Override
                public BufferedImage call() throws Exception {
                    Tensor<Float> input = maskRCNN.convertBufferedImageToTensor(originalImage);
                    if (input != null) {
                        RawDetections rawDetections = maskRCNN.executeInceptionGraph(s, input);
                        input.close();
                        Detections detections = maskRCNN.processDetections(512,512,rawDetections);
                        BufferedImage outputImage = maskRCNN.augmentDetections(originalImage, detections);
                        //  ImageIO.write(outputImage, "jpg", new File(OUTPUT_IMAGE));
                        return outputImage;
                    }
                    return null;
                }
            });
         }
        ExecutorService executor = Executors.newFixedThreadPool(1);
        try {
            executor.invokeAll(tasks);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            executor.shutdownNow();
        }

        long used = System.currentTimeMillis()-startt;
        System.out.println("time used: "+used/1000d);
    } finally {
        s.close();
        g.close();
    }
    
    long elapsedTimeInSec = (new Date().getTime() - startDate.getTime()) / 1000;
    System.out.println(String.format("Ended in %ds .", elapsedTimeInSec));
}
 
Example 7
Source File: TensorflowGraph.java    From tutorials with MIT License 4 votes vote down vote up
public static void main(String[] args) {
	Graph graph = TensorflowGraph.createGraph();
	Object result = TensorflowGraph.runGraph(graph, 3.0, 6.0);
	System.out.println(result);
	graph.close();
}