in tensorflow-examples-legacy/label_image/src/main/java/LabelImage.java [36:69]
public static void main(String[] args) throws Exception {
if (args.length < 1) {
System.err.println("USAGE: Provide a list of image filenames");
System.exit(1);
}
final List<String> labels = loadLabels();
try (Graph graph = new Graph();
Session session = new Session(graph)) {
graph.importGraphDef(loadGraphDef());
float[] probabilities = null;
for (String filename : args) {
byte[] bytes = Files.readAllBytes(Paths.get(filename));
try (Tensor<String> input = Tensors.create(bytes);
Tensor<Float> output =
session
.runner()
.feed("encoded_image_bytes", input)
.fetch("probabilities")
.run()
.get(0)
.expect(Float.class)) {
if (probabilities == null) {
probabilities = new float[(int) output.shape()[0]];
}
output.copyTo(probabilities);
int label = argmax(probabilities);
System.out.printf(
"%-30s --> %-15s (%.2f%% likely)\n",
filename, labels.get(label), probabilities[label] * 100.0);
}
}
}
}