• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 package org.tensorflow;
17 
18 import static org.junit.Assert.assertNotNull;
19 import static org.junit.Assert.assertTrue;
20 import static org.junit.Assert.fail;
21 
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.junit.runners.JUnit4;
25 
26 /** Unit tests for {@link org.tensorflow.SavedModelBundle}. */
27 @RunWith(JUnit4.class)
28 public class SavedModelBundleTest {
29 
30   private static final String SAVED_MODEL_PATH =
31       "tensorflow/cc/saved_model/testdata/half_plus_two/00000123";
32 
33   @Test
load()34   public void load() {
35     try (SavedModelBundle bundle = SavedModelBundle.load(SAVED_MODEL_PATH, "serve")) {
36       assertNotNull(bundle.session());
37       assertNotNull(bundle.graph());
38       assertNotNull(bundle.metaGraphDef());
39     }
40   }
41 
42   @Test
loadNonExistentBundle()43   public void loadNonExistentBundle() {
44     try {
45       SavedModelBundle bundle = SavedModelBundle.load("__BAD__", "serve");
46       bundle.close();
47       fail("not expected");
48     } catch (org.tensorflow.TensorFlowException e) {
49       // expected exception
50       assertTrue(e.getMessage().contains("Could not find SavedModel"));
51     }
52   }
53 
54   @Test
loader()55   public void loader() {
56     try (SavedModelBundle bundle = SavedModelBundle.loader(SAVED_MODEL_PATH)
57         .withTags("serve")
58         .withConfigProto(sillyConfigProto())
59         .withRunOptions(sillyRunOptions())
60         .load()) {
61       assertNotNull(bundle.session());
62       assertNotNull(bundle.graph());
63       assertNotNull(bundle.metaGraphDef());
64     }
65   }
66 
sillyRunOptions()67   private static byte[] sillyRunOptions() {
68     // Ideally this would use the generated Java sources for protocol buffers
69     // and end up with something like the snippet below. However, generating
70     // the Java files for the .proto files in tensorflow/core:protos_all is
71     // a bit cumbersome in bazel until the proto_library rule is setup.
72     //
73     // See https://github.com/bazelbuild/bazel/issues/52#issuecomment-194341866
74     // https://github.com/bazelbuild/rules_go/pull/121#issuecomment-251515362
75     // https://github.com/bazelbuild/rules_go/pull/121#issuecomment-251692558
76     //
77     // For this test, for now, the use of specific bytes suffices.
78     return new byte[] {0x08, 0x03};
79     /*
80     return org.tensorflow.framework.RunOptions.newBuilder()
81         .setTraceLevel(RunOptions.TraceLevel.FULL_TRACE)
82         .build()
83         .toByteArray();
84     */
85   }
86 
sillyConfigProto()87   public static byte[] sillyConfigProto() {
88     // Ideally this would use the generated Java sources for protocol buffers
89     // and end up with something like the snippet below. However, generating
90     // the Java files for the .proto files in tensorflow/core:protos_all is
91     // a bit cumbersome in bazel until the proto_library rule is setup.
92     //
93     // See https://github.com/bazelbuild/bazel/issues/52#issuecomment-194341866
94     // https://github.com/bazelbuild/rules_go/pull/121#issuecomment-251515362
95     // https://github.com/bazelbuild/rules_go/pull/121#issuecomment-251692558
96     //
97     // For this test, for now, the use of specific bytes suffices.
98     return new byte[] {0x10, 0x01, 0x28, 0x01};
99     /*
100     return org.tensorflow.framework.ConfigProto.newBuilder()
101         .setInterOpParallelismThreads(1)
102         .setIntraOpParallelismThreads(1)
103         .build()
104         .toByteArray();
105      */
106   }
107 }
108