Skip to content

Instantly share code, notes, and snippets.

@asimshankar
Created April 20, 2018 22:20
Show Gist options
  • Save asimshankar/3dc41fc75733a341319f23b6a07264df to your computer and use it in GitHub Desktop.
Save asimshankar/3dc41fc75733a341319f23b6a07264df to your computer and use it in GitHub Desktop.
TensorFlow Java: Loading SavedModels from GCS
// File placed in src/main/java/HelloTF.java
import org.tensorflow.SavedModelBundle;
import org.tensorflow.TensorFlow;
public class HelloTF {
public static void main(String[] args) throws Exception {
System.out.print("Loading: ");
System.out.println(args[0]);
try (SavedModelBundle model = SavedModelBundle.load(args[0], "serve")) {
System.out.format("TensorFlow version: %s, Model MetaGraphLength: %d\n",
TensorFlow.version(), model.metaGraphDef().length);
}
}
}
Loading: gs://....
2018-04-20 15:19:08.477888: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2018-04-20 15:19:09.759204: I tensorflow/cc/saved_model/loader.cc:242] Loading SavedModel with tags: { serve }; from: gs://...
2018-04-20 15:19:10.254415: I tensorflow/cc/saved_model/loader.cc:161] Restoring SavedModel bundle.
2018-04-20 15:19:11.575483: I tensorflow/cc/saved_model/loader.cc:196] Running LegacyInitOp on SavedModel bundle.
2018-04-20 15:19:11.576887: I tensorflow/cc/saved_model/loader.cc:291] SavedModel load for tags { serve }; Status: success. Took 3098430 microseconds.
TensorFlow version: 1.7.0, Model MetaGraphLength: 57035
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.myorg</groupId>
<artifactId>hello-tf</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<exec.mainClass>HelloTF</exec.mainClass>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow</artifactId>
<version>1.7.0</version>
</dependency>
</dependencies>
</project>
# Provide credentials as per:
# https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application
export GOOGLE_APPLICATION_CREDENTIALS=/tmp/credentials.json
# Compile and run
mvn -q clean compile exec:java -Dexec.args="gs://<FULL PATH HERE>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment