org.tensorflow.framework.MetaGraphDef Java Examples

The following examples show how to use org.tensorflow.framework.MetaGraphDef. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TensorFlowModel.java    From zoltar with Apache License 2.0 5 votes vote down vote up
/**
 * Note: Please use Models from zoltar-models module.
 *
 * <p>Returns a TensorFlow model with metadata given {@link SavedModelBundle} export directory URI
 * and {@link Options}.
 */
public static TensorFlowModel create(
    final Model.Id id,
    final URI modelResource,
    final Options options,
    final String signatureDefinition)
    throws IOException {
  // GCS requires that directory URIs have a trailing slash, so add the slash if it's missing
  // and the URI starts with 'gs'.
  final URI normalizedUri =
      !CloudStorageFileSystem.URI_SCHEME.equalsIgnoreCase(modelResource.getScheme())
              || modelResource.toString().endsWith("/")
          ? modelResource
          : URI.create(modelResource.toString() + "/");
  final URI localDir = FileSystemExtras.downloadIfNonLocal(normalizedUri);
  final SavedModelBundle model =
      SavedModelBundle.load(localDir.toString(), options.tags().toArray(new String[0]));
  final MetaGraphDef metaGraphDef;
  try {
    metaGraphDef = extractMetaGraphDefinition(model);
  } catch (TensorflowMetaGraphDefParsingException e) {
    throw new IOException(e);
  }
  final SignatureDef signatureDef = metaGraphDef.getSignatureDefOrThrow(signatureDefinition);

  return new AutoValue_TensorFlowModel(
      id,
      model,
      options,
      metaGraphDef,
      signatureDef,
      toNameMap(signatureDef.getInputsMap()),
      toNameMap(signatureDef.getOutputsMap()));
}
 
Example #2
Source File: TensorFlowModel.java    From zoltar with Apache License 2.0 5 votes vote down vote up
private static MetaGraphDef extractMetaGraphDefinition(final SavedModelBundle bundle)
    throws TensorflowMetaGraphDefParsingException {
  final MetaGraphDef metaGraphDef;
  try {
    metaGraphDef = MetaGraphDef.parseFrom(bundle.metaGraphDef());
  } catch (InvalidProtocolBufferException e) {
    throw new TensorflowMetaGraphDefParsingException(
        "Failed parsing tensorflow metagraph " + "definition", e);
  }

  return metaGraphDef;
}
 
Example #3
Source File: SavedModel.java    From jpmml-tensorflow with GNU Affero General Public License v3.0 5 votes vote down vote up
public CollectionDef getCollectionDef(String key){
	MetaGraphDef metaGraphDef = getMetaGraphDef();

	Map<String, CollectionDef> collectionMap = metaGraphDef.getCollectionDefMap();

	CollectionDef collectionDef = collectionMap.get(key);
	if(collectionDef == null){
		throw new IllegalArgumentException(key);
	}

	return collectionDef;
}
 
Example #4
Source File: GraphImporter.java    From vespa with Apache License 2.0 5 votes vote down vote up
static IntermediateGraph importGraph(String modelName, SavedModelBundle bundle) throws IOException {
    MetaGraphDef tfGraph = MetaGraphDef.parseFrom(bundle.metaGraphDef());

    IntermediateGraph intermediateGraph = new IntermediateGraph(modelName);
    importSignatures(tfGraph, intermediateGraph);
    importOperations(tfGraph, intermediateGraph, bundle);
    verifyOutputTypes(tfGraph, intermediateGraph);

    return intermediateGraph;
}
 
Example #5
Source File: GraphImporter.java    From vespa with Apache License 2.0 5 votes vote down vote up
private static void importOperations(MetaGraphDef tfGraph,
                                     IntermediateGraph intermediateGraph,
                                     SavedModelBundle bundle) {
    for (String signatureName : intermediateGraph.signatures()) {
        for (String outputName : intermediateGraph.outputs(signatureName).values()) {
            importOperation(outputName, tfGraph.getGraphDef(), intermediateGraph, bundle);
        }
    }
}
 
Example #6
Source File: GraphImporter.java    From vespa with Apache License 2.0 5 votes vote down vote up
private static void verifyOutputTypes(MetaGraphDef tfGraph, IntermediateGraph intermediateGraph) {
    for (String signatureName : intermediateGraph.signatures()) {
        for (String outputName : intermediateGraph.outputs(signatureName).values()) {
            IntermediateOperation operation = intermediateGraph.get(outputName);
            NodeDef node = getTensorFlowNodeFromGraph(IntermediateOperation.namePartOf(operation.name()), tfGraph.getGraphDef());
            OrderedTensorType type = operation.type().orElseThrow(
                    () -> new IllegalArgumentException("Output of '" + outputName + "' has no type."));
            TypeConverter.verifyType(node, type);
        }
    }

}
 
Example #7
Source File: SavedModel.java    From jpmml-tensorflow with GNU Affero General Public License v3.0 4 votes vote down vote up
public MetaGraphDef getMetaGraphDef(){
	return this.metaGraphDef;
}
 
Example #8
Source File: SavedModel.java    From jpmml-tensorflow with GNU Affero General Public License v3.0 4 votes vote down vote up
private void setMetaGraphDef(MetaGraphDef metaGraphDef){
	this.metaGraphDef = metaGraphDef;
}
 
Example #9
Source File: TensorFlowModel.java    From zoltar with Apache License 2.0 votes vote down vote up
public abstract MetaGraphDef metaGraphDefinition();