All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tensorflow.SavedModelBundle Maven / Gradle / Ivy

There is a newer version: 1.15.0
Show newest version
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

package org.tensorflow;

/**
 * SavedModelBundle represents a model loaded from storage.
 *
 * 

The model consists of a description of the computation (a {@link Graph}), a {@link Session} * with tensors (e.g., parameters or variables in the graph) initialized to values saved in storage, * and a description of the model (a serialized representation of a MetaGraphDef * protocol buffer). */ public class SavedModelBundle implements AutoCloseable { /** Options for loading a SavedModel. */ public static final class Loader { /** Load a SavedModelBundle with the configured options. */ public SavedModelBundle load() { return SavedModelBundle.load(exportDir, tags, configProto, runOptions); } /** * Sets options to use when executing model initialization operations. * * @param options Serialized RunOptions * protocol buffer. */ public Loader withRunOptions(byte[] options) { this.runOptions = options; return this; } /** * Set configuration of the Session object created when loading the model. * * @param configProto Serialized ConfigProto * protocol buffer. */ public Loader withConfigProto(byte[] configProto) { this.configProto = configProto; return this; } /** * Sets the set of tags that identify the specific graph in the saved model to load. * * @param tags the tags identifying the specific MetaGraphDef to load. */ public Loader withTags(String... tags) { this.tags = tags; return this; } private Loader(String exportDir) { this.exportDir = exportDir; } private String exportDir = null; private String[] tags = null; private byte[] configProto = null; private byte[] runOptions = null; } /** * Load a saved model from an export directory. The model that is being loaded should be created * using the Saved Model * API. * *

This method is a shorthand for: * *

{@code
   * SavedModelBundler.loader().withTags(tags).load();
   * }
* * @param exportDir the directory path containing a saved model. * @param tags the tags identifying the specific metagraphdef to load. * @return a bundle containing the graph and associated session. */ public static SavedModelBundle load(String exportDir, String... tags) { return loader(exportDir).withTags(tags).load(); } /** * Load a saved model. * *

Returns a Loader object that can set configuration options before actually * loading the model, * * @param exportDir the directory path containing a saved model. */ public static Loader loader(String exportDir) { return new Loader(exportDir); } /** * Returns the serialized MetaGraphDef * protocol buffer associated with the saved model. */ public byte[] metaGraphDef() { return metaGraphDef; } /** Returns the graph that describes the computation performed by the model. */ public Graph graph() { return graph; } /** * Returns the {@link Session} with which to perform computation using the model. * * @return the initialized session */ public Session session() { return session; } /** * Releases resources (the {@link Graph} and {@link Session}) associated with the saved model * bundle. */ @Override public void close() { session.close(); graph.close(); } private final Graph graph; private final Session session; private final byte[] metaGraphDef; private SavedModelBundle(Graph graph, Session session, byte[] metaGraphDef) { this.graph = graph; this.session = session; this.metaGraphDef = metaGraphDef; } /** * Create a SavedModelBundle object from a handle to the C TF_Graph object and to the C TF_Session * object, plus the serialized MetaGraphDef. * *

Invoked from the native load method. Takes ownership of the handles. */ private static SavedModelBundle fromHandle( long graphHandle, long sessionHandle, byte[] metaGraphDef) { Graph graph = new Graph(graphHandle); Session session = new Session(graph, sessionHandle); return new SavedModelBundle(graph, session, metaGraphDef); } private static native SavedModelBundle load( String exportDir, String[] tags, byte[] config, byte[] runOptions); static { TensorFlow.init(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy