Search in sources :

Example 1 with GraphBuilder

use of edu.ml.tensorflow.util.GraphBuilder in project tensorflow-java-yolo 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)

Example 2 with GraphBuilder

use of edu.ml.tensorflow.util.GraphBuilder 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

GraphBuilder (edu.ml.tensorflow.util.GraphBuilder)2 Graph (org.tensorflow.Graph)2 Session (org.tensorflow.Session)2