Skip to content

Commit

Permalink
chore: add example predict_mnist.java
Browse files Browse the repository at this point in the history
  • Loading branch information
tadayosi committed Dec 14, 2024
1 parent 0a70225 commit 852ea2e
Show file tree
Hide file tree
Showing 99 changed files with 88 additions and 0 deletions.
Binary file added data/mnist/0/10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/0/13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/0/25.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/0/28.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/0/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/0/55.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/0/69.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/0/71.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/1/14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/1/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/1/29.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/1/31.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/1/37.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/1/39.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/1/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/2/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/2/35.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/2/43.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/2/47.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/2/72.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/2/77.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/2/82.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/3/18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/3/30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/3/32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/mnist/3/44.png
Binary file added data/mnist/3/51.png
Binary file added data/mnist/3/63.png
Binary file added data/mnist/3/68.png
Binary file added data/mnist/3/76.png
Binary file added data/mnist/3/87.png
Binary file added data/mnist/3/90.png
Binary file added data/mnist/3/93.png
Binary file added data/mnist/4/19.png
Binary file added data/mnist/4/24.png
Binary file added data/mnist/4/27.png
Binary file added data/mnist/4/33.png
Binary file added data/mnist/4/4.png
Binary file added data/mnist/4/42.png
Binary file added data/mnist/4/48.png
Binary file added data/mnist/4/49.png
Binary file added data/mnist/4/56.png
Binary file added data/mnist/4/6.png
Binary file added data/mnist/4/65.png
Binary file added data/mnist/4/67.png
Binary file added data/mnist/4/85.png
Binary file added data/mnist/4/95.png
Binary file added data/mnist/5/15.png
Binary file added data/mnist/5/23.png
Binary file added data/mnist/5/45.png
Binary file added data/mnist/5/52.png
Binary file added data/mnist/5/53.png
Binary file added data/mnist/5/59.png
Binary file added data/mnist/5/8.png
Binary file added data/mnist/6/11.png
Binary file added data/mnist/6/21.png
Binary file added data/mnist/6/22.png
Binary file added data/mnist/6/50.png
Binary file added data/mnist/6/54.png
Binary file added data/mnist/6/66.png
Binary file added data/mnist/6/81.png
Binary file added data/mnist/6/88.png
Binary file added data/mnist/6/91.png
Binary file added data/mnist/6/98.png
Binary file added data/mnist/7/0.png
Binary file added data/mnist/7/17.png
Binary file added data/mnist/7/26.png
Binary file added data/mnist/7/34.png
Binary file added data/mnist/7/36.png
Binary file added data/mnist/7/41.png
Binary file added data/mnist/7/60.png
Binary file added data/mnist/7/64.png
Binary file added data/mnist/7/70.png
Binary file added data/mnist/7/75.png
Binary file added data/mnist/7/79.png
Binary file added data/mnist/7/80.png
Binary file added data/mnist/7/83.png
Binary file added data/mnist/7/86.png
Binary file added data/mnist/7/97.png
Binary file added data/mnist/8/110.png
Binary file added data/mnist/8/128.png
Binary file added data/mnist/8/134.png
Binary file added data/mnist/8/146.png
Binary file added data/mnist/8/177.png
Binary file added data/mnist/8/179.png
Binary file added data/mnist/8/61.png
Binary file added data/mnist/8/84.png
Binary file added data/mnist/9/12.png
Binary file added data/mnist/9/16.png
Binary file added data/mnist/9/20.png
Binary file added data/mnist/9/58.png
Binary file added data/mnist/9/62.png
Binary file added data/mnist/9/7.png
Binary file added data/mnist/9/73.png
Binary file added data/mnist/9/78.png
Binary file added data/mnist/9/9.png
Binary file added data/mnist/9/92.png
Binary file added data/mnist/9/99.png
88 changes: 88 additions & 0 deletions examples/predict_mnist.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
///usr/bin/env jbang "$0" "$@" ; exit $?

//JAVA 17+
//DEPS io.github.tadayosi.tensorflow:tensorflow-serving-client:0.2.0
//DEPS org.slf4j:slf4j-simple:2.0.16

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

import javax.imageio.ImageIO;

import org.tensorflow.framework.DataType;
import org.tensorflow.framework.TensorProto;
import org.tensorflow.framework.TensorShapeProto;
import org.tensorflow.framework.TensorShapeProto.Dim;

import com.google.protobuf.Int64Value;

import io.github.tadayosi.tensorflow.serving.client.TensorFlowServingClient;
import tensorflow.serving.Model.ModelSpec;
import tensorflow.serving.Predict.PredictRequest;

public class predict_mnist {

static final String DATA_DIR = "data/mnist";

public static void main(String... args) throws Exception {
try (var client = TensorFlowServingClient.newInstance()) {
Files.walk(Path.of(DATA_DIR)).forEach(file -> {
if (Files.isDirectory(file)) {
System.out.println("Directory: " + file.getFileName());
return;
}
try {
var data = preprocess(file);
var inputs = TensorProto.newBuilder()
.setDtype(DataType.DT_FLOAT)
.setTensorShape(TensorShapeProto.newBuilder()
.addDim(Dim.newBuilder().setSize(28))
.addDim(Dim.newBuilder().setSize(28)))
.addAllFloatVal(data)
.build();
var request = PredictRequest.newBuilder()
.setModelSpec(ModelSpec.newBuilder()
.setName("mnist")
.setVersion(Int64Value.of(1)))
.putInputs("keras_tensor", inputs)
.build();
var response = client.predict(request);
var output = response.getOutputsOrThrow("output_0");
var answer = argmax(output);
System.out.println(" %s => %s".formatted(file.getFileName(), answer));
} catch (IOException e) {
e.printStackTrace();
}
});
}
}

static List<Float> preprocess(Path file) throws IOException {
var image = ImageIO.read(file.toFile());
var width = image.getWidth();
var height = image.getHeight();
if (width != 28 || height != 28) {
throw new IllegalArgumentException("Image size must be 28x28");
}
var normalised = new Float[width * height];
var i = 0;
for (var y = 0; y < height; y++) {
for (var x = 0; x < width; x++) {
var rgb = image.getRGB(x, y);
normalised[i++] = (rgb & 0xFF) / 255.0f;
}
}
return Arrays.asList(normalised);
}

static int argmax(TensorProto tensor) {
return IntStream.range(0, tensor.getFloatValCount())
.reduce((max, i) -> tensor.getFloatVal(max) > tensor.getFloatVal(i) ? max : i)
.orElseThrow();
}

}

0 comments on commit 852ea2e

Please sign in to comment.