从 TFLite 模型 FlatBuffer 加载元数据。
TFLite 模型 FlatBuffer 可以使用 TFLite 模型架构文件生成。
某些模型包含 TFLite 元数据 Flatbuffer,用于记录有关模型功能以及如何解读模型的更多信息。TFLite 元数据 Flatbuffer 可以使用 TFLite 元数据架构文件生成。
允许在没有 TFLite 元数据的情况下传入模型 FlatBuffer。但是,调用从 TFLite 元数据读取的方法会导致运行时错误。
同样,允许在没有关联文件的情况下传入模型 FlatBuffer。但是,调用读取关联文件的方法会导致运行时错误。
虽然 TFLite 模型 FlatBuffer 支持多个子图,但到目前为止,TFLite 解释器仅支持一个子图。如需了解详情,请参阅有关如何在转换期间指定子图的说明。因此,MetadataExtractor
在其方法中省略了子图索引作为输入。
嵌套类
类别 | MetadataExtractor.QuantizationParams | 与 TFLite 模型架构文件 中的表 QuantizationParameters 对应的量化参数。 |
公共构造函数
公共方法
InputStream | |
Set<String>> |
getAssociatedFileNames()
获取关联文件的文件名。
|
整型 |
getInputTensorCount()
获取模型中输入张量的计数。
|
TensorMetadata |
getInputTensorMetadata(int inputIndex)
获取由
inputIndex 指定的输入张量的元数据。 |
MetadataExtractor.QuantizationParams |
getInputTensorQuantizationParams(int inputIndex)
获取由
inputIndex 指定的输入张量的量化参数。 |
整型 [] |
getInputTensorShape(int inputIndex)
使用
inputIndex 获取输入张量的形状。 |
字节 |
getInputTensorType(int inputIndex)
使用
inputIndex 获取输入张量的 ERROR(/TensorType) 。 |
ModelMetadata |
getModelMetadata()
获取模型元数据的根处理程序。
|
整型 |
getOutputTensorCount()
获取模型中输出张量的计数。
|
TensorMetadata |
getOutputTensorMetadata(int outputIndex)
获取由
outputIndex 指定的输出张量的元数据。 |
MetadataExtractor.QuantizationParams |
getOutputTensorQuantizationParams(int outputIndex)
获取由
outputIndex 指定的输出张量的量化参数。 |
整型 [] |
getOutputTensorShape(int outputIndex)
使用
outputIndex 获取输出张量的形状。 |
字节 |
getOutputTensorType(int outputIndex)
使用
outputIndex 获取输出张量的 ERROR(/TensorType) 。 |
布尔值 |
hasMetadata()
如果模型具有元数据,则返回
true 。 |
final 布尔值 |
isMinimumParserVersionSatisfied()
如果给定元数据 FlatBuffer 所需的最低解析器版本早于或等于此 MetadataExtractor 库所依赖的元数据解析器版本,则返回
true 。 |
继承的方法
公共构造函数
public MetadataExtractor (ByteBuffer 缓冲区)
使用 TFLite 模型 FlatBuffer 创建 MetadataExtractor
。
参数
缓存空间 | TFLite 模型 FlatBuffer |
---|
抛出
IllegalArgumentException | 如果模型中的输入或输出张量数量与元数据中的数量不匹配 |
---|---|
IOException | 在以 Zip 文件的形式读取模型时出错 |
公共方法
public InputStream getAssociatedFile (String fileName)
获取具有指定 fileName
的打包的关联文件。
参数
fileName | 关联文件的名称 |
---|
返回
- 包含指定文件的原始输入流
抛出
IllegalStateException | 如果模型不是 ZIP 文件 |
---|---|
IllegalArgumentException | 如果模型中不存在指定的文件, |
public int getInputTensorCount ()
获取模型中输入张量的计数。
public TensorMetadata getInputTensorMetadata (int inputIndex)
public MetadataExtractor.QuantizationParams getInputTensorQuantizationParams (int inputIndex)
获取由 inputIndex
指定的输入张量的量化参数。
参数
inputIndex | 所需输入张量的索引 |
---|
public int[] getInputTensorShape (int inputIndex)
使用 inputIndex
获取输入张量的形状。
参数
inputIndex | 所需输入张量的索引 |
---|
public byte getInputTensorType (int inputIndex)
使用 inputIndex
获取输入张量的 ERROR(/TensorType)
。
参数
inputIndex | 所需输入张量的索引 |
---|
public int getOutputTensorCount ()
获取模型中输出张量的计数。
public TensorMetadata getOutputTensorMetadata (int outputIndex)
public MetadataExtractor.QuantizationParams getOutputTensorQuantizationParams (int outputIndex)
获取由 outputIndex
指定的输出张量的量化参数。
参数
outputIndex | 所需输出张量的索引 |
---|
public int[] getOutputTensorShape (int outputIndex)
使用 outputIndex
获取输出张量的形状。
参数
outputIndex | 所需输出张量的索引 |
---|
public byte getOutputTensorType (int outputIndex)
使用 outputIndex
获取输出张量的 ERROR(/TensorType)
。
参数
outputIndex | 所需输出张量的索引 |
---|
public 布尔值 hasMetadata ()
如果模型具有元数据,则返回 true
。否则,返回 false
。
public final final 布尔值 isMinimumParserVersionSatisfied ()
如果给定元数据 FlatBuffer 所需的最低解析器版本早于或等于此 MetadataExtractor 库所依赖的元数据解析器版本,则返回 true
。在这种情况下,可以使用此元数据提取器库正确解析元数据中的所有字段。否则,它会返回 false
。
例如,假设底层元数据解析器的版本为 1.14.1
,
- 如果所需的最低解析器版本相同或更低,例如
1.14.1
或1.14.0
,则它返回true
。null 版本优先于所有数字版本,因为某些元数据平面缓冲区是在第一个版本化版本之前生成的; - 如果所需的最低解析器版本较新(例如
1.14.2
),则返回false
。