Search in sources :

Example 11 with Graph

use of org.tensorflow.Graph in project tensorflow-example-java by szaza.

the class ObjectDetector method normalizeImage.

/**
 * Pre-process input. It resize the image and normalize its pixels
 * @param imageBytes Input image
 * @return Tensor<Float> with shape [1][416][416][3]
 */
private Tensor<Float> normalizeImage(final byte[] imageBytes) {
    try (Graph graph = new Graph()) {
        GraphBuilder graphBuilder = new GraphBuilder(graph);
        final Output<Float> output = // Divide each pixels with the MEAN
        graphBuilder.div(// Resize using bilinear interpolation
        graphBuilder.resizeBilinear(// Increase the output tensors dimension
        graphBuilder.expandDims(// Cast the output to Float
        graphBuilder.cast(graphBuilder.decodeJpeg(graphBuilder.constant("input", imageBytes), 3), Float.class), graphBuilder.constant("make_batch", 0)), graphBuilder.constant("size", new int[] { SIZE, SIZE })), graphBuilder.constant("scale", MEAN));
        try (Session session = new Session(graph)) {
            return session.runner().fetch(output.op().name()).run().get(0).expect(Float.class);
        }
    }
}
Also used : Graph(org.tensorflow.Graph) GraphBuilder(edu.ml.tensorflow.util.GraphBuilder) Session(org.tensorflow.Session)

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