MetadataExtractor

公用類別 MetadataExtractor

從 TFLite Model FlatBuffer 載入中繼資料。

您可以使用 TFLite Model 結構定義檔案產生 TFLite Model FlatBuffer。

部分模型包含 TFLite 中繼資料 Flatbuffer,用於記錄模型功能與解讀方式的詳細資訊。您可以使用 TFLite 中繼資料結構定義檔案產生 TFLite 中繼資料平面緩衝區。

允許在沒有 TFLite 中繼資料的情況下,傳入 FlatBuffer 的模型。不過,叫用 TFLite 中繼資料讀取的方法將會導致執行階段錯誤。

同樣地,它也可在沒有關聯檔案的情況下,傳入模型 FlatBuffer。但是,叫用會讀取相關檔案的方法將會導致執行階段錯誤。

雖然 TFLite 模型 FlatBuffer 支援多個子圖表,但 TFLite 翻譯工具目前只支援一個子圖表。詳情請參閱如何在轉換期間指定子圖表的操作說明。因此,MetadataExtractor 會在其方法中省略子圖表索引做為輸入內容。

巢狀類別

類別 MetadataExtractor.QuantizationParams TFLite Model 結構定義檔案中 資料表 QuantizationParameters 對應的量化參數。

公用建構函式

MetadataExtractor(ByteBuffer 緩衝區)
使用 TFLite 模型 FlatBuffer 建立 MetadataExtractor

公用方法

InputStream
getAssociatedFile(String fileName)
取得具有指定 fileName 的封裝關聯檔案。
Set<String>
getAssociatedFileNames()
取得關聯檔案的檔案名稱。
int
getInputTensorCount()
取得模型中的輸入張量數量。
TensorMetadata
getInputTensorMetadata(int inputIndex)
取得 inputIndex 指定的輸入張量中繼資料。
MetadataExtractor.QuantizationParams
getInputTensorQuantizationParams(int inputIndex)
取得 inputIndex 指定的輸入張量的量化參數。
int[]
getInputTensorShape(int inputIndex)
透過 inputIndex 取得輸入張量的形狀。
位元組
getInputTensorType(int inputIndex)
透過 inputIndex 取得輸入張量的 ERROR(/TensorType)
ModelMetadata
getModelMetadata()
取得模型中繼資料的根處理常式。
int
getOutputTensorCount()
取得模型的輸出張量數量。
TensorMetadata
getOutputTensorMetadata(int outputIndex)
取得 outputIndex 指定的輸出張量中繼資料。
MetadataExtractor.QuantizationParams
getOutputTensorQuantizationParams(int outputIndex)
針對 outputIndex 指定的輸出張量,取得量化參數。
int[]
getOutputTensorShape(int outputIndex)
使用 outputIndex 取得輸出張量的形狀。
位元組
getOutputTensorType(int outputIndex)
使用 outputIndex 取得輸出張量的 ERROR(/TensorType)
布林值
hasMetadata()
如果模型有中繼資料,則傳回 true
最終 布林值
isMinimumParserVersionSatisfied()
如果指定中繼資料扁平緩衝區所需的最低剖析器版本,早於或等於這個 MetadataExtractor 程式庫依賴的中繼資料剖析器版本,則傳回 true

繼承的方法

公用建構函式

public MetadataExtractor (ByteBuffer 緩衝區)

使用 TFLite 模型 FlatBuffer 建立 MetadataExtractor

參數
緩衝區 TFLite 模型 FlatBuffer
擲回
IllegalArgumentException 如果模型中的輸入或輸出張量數量與中繼資料不符
IOException 如果在以 ZIP 檔案的形式讀取模型時發生錯誤

公用方法

public InputStream getAssociatedFile (String fileName)

取得具有指定 fileName 的封裝關聯檔案。

參數
fileName 相關檔案名稱
傳回
  • 含有指定檔案的原始輸入串流
擲回
IllegalStateException 如果模型不是 ZIP 檔案
IllegalArgumentException 表示指定檔案不存在模型中

public Set<String> getAssociatedFileNames ()

取得關聯檔案的檔案名稱。

傳回
  • 相關檔案名稱
擲回
IllegalStateException 如果模型不是 ZIP 檔案

public int getInputTensorCount ()

取得模型中的輸入張量數量。

public TensorMetadata getInputTensorMetadata (int inputIndex)

取得 inputIndex 指定的輸入張量中繼資料。

參數
inputIndex 所需輸入張量的索引
擲回
IllegalStateException 不含模型中繼資料

public MetadataExtractor.QuantizationParams getInputTensorQuantizationParams (int inputIndex)

取得 inputIndex 指定的輸入張量的量化參數。

參數
inputIndex 所需輸入張量的索引

public int[] getInputTensorShape (int inputIndex)

透過 inputIndex 取得輸入張量的形狀。

參數
inputIndex 所需輸入張量的索引

public 位元組 getInputTensorType (int inputIndex)

透過 inputIndex 取得輸入張量的 ERROR(/TensorType)

參數
inputIndex 所需輸入張量的索引

public ModelMetadata getModelMetadata ()

取得模型中繼資料的根處理常式。

擲回
IllegalStateException 不含模型中繼資料

public int getOutputTensorCount ()

取得模型的輸出張量數量。

public TensorMetadata getOutputTensorMetadata (int outputIndex)

取得 outputIndex 指定的輸出張量中繼資料。

參數
outputIndex 所需輸出張量的索引
擲回
IllegalStateException 不含模型中繼資料

public MetadataExtractor.QuantizationParams getOutputTensorQuantizationParams (int outputIndex)

針對 outputIndex 指定的輸出張量,取得量化參數。

參數
outputIndex 所需輸出張量的索引

public int[] getOutputTensorShape (int outputIndex)

使用 outputIndex 取得輸出張量的形狀。

參數
outputIndex 所需輸出張量的索引

public 位元組 getOutputTensorType (int outputIndex)

使用 outputIndex 取得輸出張量的 ERROR(/TensorType)

參數
outputIndex 所需輸出張量的索引

public 布林值 hasMetadata ()

如果模型有中繼資料,則傳回 true。如果沒有,則傳回 false

public 最終 布林值 isMinimumParserVersionSatisfied ()

如果指定中繼資料扁平緩衝區所需的最低剖析器版本,早於或等於這個 MetadataExtractor 程式庫依賴的中繼資料剖析器版本,則傳回 true。在此情況下,您可以利用這個中繼資料擷取器程式庫,正確剖析中繼資料中的所有欄位。否則,則會傳回 false

舉例來說,假設基礎中繼資料剖析器版本為 1.14.1

  • 如果必要的剖析器版本已相同或更舊,例如 1.14.11.14.0,則會傳回 true。空值版本早於所有數字版本,因為系統會在首次發布版本前產生一些中繼資料扁平緩衝區;
  • 如果所需的剖析器版本低於最低版本,則會傳回 false,例如 1.14.2