Help protect the Great Barrier Reef with TensorFlow on Kaggle Join Challenge

MetadataExtractor

public class MetadataExtractor

Loads metadata from TFLite Model FlatBuffer.

TFLite Model FlatBuffer can be generated using the TFLite Model schema file.

Some models contain a TFLite Metadata Flatbuffer, which records more information about what the model does and how to interprete the model. TFLite Metadata Flatbuffer can be generated using the TFLite Metadata schema file.

It is allowed to pass in a model FlatBuffer without TFLite metadata. However, invoking methods that read from TFLite metadata will cause runtime errors.

Similarly, it is allowed to pass in a model FlatBuffer without associated files. However, invoking methods that read the associated files will cause runtime errors.

Though TFLite model FlatBuffer supports multiple subgraphs, TFLite Interpreter only supports a single subgraph so far. See the instruction of how to specify subgraph during convertion for more information. Therefore, MetadataExtractor omits subgraph index as an input in its methods.

Nested Classes

class MetadataExtractor.QuantizationParams Quantization parameters that corresponds to the table, QuantizationParameters, in the TFLite Model schema file. 

Public Constructors

MetadataExtractor(ByteBuffer buffer)
Creates a MetadataExtractor with TFLite model FlatBuffer.

Public Methods

InputStream
getAssociatedFile(String fileName)
Gets the packed associated file with the specified fileName.
Set<String>
getAssociatedFileNames()
Gets the file names of the associated files.
int
getInputTensorCount()
Gets the count of input tensors in the model.
TensorMetadata
getInputTensorMetadata(int inputIndex)
Gets the metadata for the input tensor specified by inputIndex.
MetadataExtractor.QuantizationParams
getInputTensorQuantizationParams(int inputIndex)
Gets the quantization parameters for the input tensor specified by inputIndex.
int[]
getInputTensorShape(int inputIndex)
Gets the shape of the input tensor with inputIndex.
byte
getInputTensorType(int inputIndex)
Gets the ERROR(/TensorType) of the input tensor with inputIndex.
ModelMetadata
getModelMetadata()
Gets the root handler for the model metadata.
int
getOutputTensorCount()
Gets the count of output tensors in the model.
TensorMetadata
getOutputTensorMetadata(int outputIndex)
Gets the metadata for the output tensor specified by outputIndex.
MetadataExtractor.QuantizationParams
getOutputTensorQuantizationParams(int outputIndex)
Gets the quantization parameters for the output tensor specified by outputIndex.
int[]
getOutputTensorShape(int outputIndex)
Gets the shape of the output tensor with outputIndex.
byte
getOutputTensorType(int outputIndex)
Gets the ERROR(/TensorType) of the output tensor with outputIndex.
boolean
hasMetadata()
Returns true if the model has metadata.
final boolean
isMinimumParserVersionSatisfied()
Returns true if the minimum parser version required by the given metadata flatbuffer precedes or equals to the version of the metadata parser that this MetadataExtractor library is relying on.

Inherited Methods

Public Constructors

public MetadataExtractor (ByteBuffer buffer)

Creates a MetadataExtractor with TFLite model FlatBuffer.

Parameters
buffer the TFLite model FlatBuffer
Throws
IllegalArgumentException if the number of input or output tensors in the model does not match that in the metadata
IOException if an error occurs while reading the model as a Zip file

Public Methods

public InputStream getAssociatedFile (String fileName)

Gets the packed associated file with the specified fileName.

Parameters
fileName the name of the associated file
Returns
  • the raw input stream containing specified file
Throws
IllegalStateException if the model is not a zip file
IllegalArgumentException if the specified file does not exist in the model

public Set<String> getAssociatedFileNames ()

Gets the file names of the associated files.

Returns
  • the file names of the associated files
Throws
IllegalStateException if the model is not a zip file

public int getInputTensorCount ()

Gets the count of input tensors in the model.

public TensorMetadata getInputTensorMetadata (int inputIndex)

Gets the metadata for the input tensor specified by inputIndex.

Parameters
inputIndex the index of the desired input tensor
Throws
IllegalStateException if this model does not contain model metadata

public MetadataExtractor.QuantizationParams getInputTensorQuantizationParams (int inputIndex)

Gets the quantization parameters for the input tensor specified by inputIndex.

Parameters
inputIndex the index of the desired input tensor

public int[] getInputTensorShape (int inputIndex)

Gets the shape of the input tensor with inputIndex.

Parameters
inputIndex the index of the desired input tensor

public byte getInputTensorType (int inputIndex)

Gets the ERROR(/TensorType) of the input tensor with inputIndex.

Parameters
inputIndex the index of the desired input tensor

public ModelMetadata getModelMetadata ()

Gets the root handler for the model metadata.

Throws
IllegalStateException if this model does not contain model metadata

public int getOutputTensorCount ()

Gets the count of output tensors in the model.

public TensorMetadata getOutputTensorMetadata (int outputIndex)

Gets the metadata for the output tensor specified by outputIndex.

Parameters
outputIndex the index of the desired output tensor
Throws
IllegalStateException if this model does not contain model metadata

public MetadataExtractor.QuantizationParams getOutputTensorQuantizationParams (int outputIndex)

Gets the quantization parameters for the output tensor specified by outputIndex.

Parameters
outputIndex the index of the desired output tensor

public int[] getOutputTensorShape (int outputIndex)

Gets the shape of the output tensor with outputIndex.

Parameters
outputIndex the index of the desired output tensor

public byte getOutputTensorType (int outputIndex)

Gets the ERROR(/TensorType) of the output tensor with outputIndex.

Parameters
outputIndex the index of the desired output tensor

public boolean hasMetadata ()

Returns true if the model has metadata. Otherwise, returns false.

public final boolean isMinimumParserVersionSatisfied ()

Returns true if the minimum parser version required by the given metadata flatbuffer precedes or equals to the version of the metadata parser that this MetadataExtractor library is relying on. All fields in the metadata can be parsed correctly with this metadata extractor library in this case. Otherwise, it returns false.

For example, assume the underlying metadata parser version is 1.14.1,

  • it returns true, if the required minimum parser version is the same or older, such as 1.14.1 or 1.14.0. Null version precedes all numeric versions, because some metadata flatbuffers are generated before the first versioned release;
  • it returns false, if the required minimum parser version is newer, such as 1.14.2.