Konvertoni modelet TensorFlow, Konvertoni modelet TensorFlow

Kjo faqe përshkruan se si të konvertohet një model TensorFlow në një model LiteRT (një format i optimizuar FlatBuffer i identifikuar nga prapashtesa e skedarit .tflite ) duke përdorur konvertuesin LiteRT.

Fluksi i punës së konvertimit

Diagrama më poshtë ilustron rrjedhën e punës së nivelit të lartë për konvertimin e modelit tuaj:

Fluksi i punës së konvertuesit TFLite. ...

Figura 1. Fluksi i punës së konvertuesit.

Ju mund ta konvertoni modelin tuaj duke përdorur një nga opsionet e mëposhtme:

  1. Python API ( i rekomanduar ): Kjo ju lejon të integroni konvertimin në tubacionin tuaj të zhvillimit, të aplikoni optimizime, të shtoni meta të dhëna dhe shumë detyra të tjera që thjeshtojnë procesin e konvertimit.
  2. Rreshti i komandës : Kjo mbështet vetëm konvertimin bazë të modelit.

API-ja e Python-it

Kodi ndihmës: Për të mësuar më shumë rreth API-t të konvertuesit LiteRT, ekzekutoni print(help(tf.lite.TFLiteConverter)) .

Konvertoni një model TensorFlow duke përdorur tf.lite.TFLiteConverter . Një model TensorFlow ruhet duke përdorur formatin SavedModel dhe gjenerohet ose duke përdorur API-të e nivelit të lartë tf.keras.* (një model Keras) ose API-të e nivelit të ulët tf.* (nga të cilat gjeneroni funksione konkrete). Si rezultat, keni tre opsionet e mëposhtme (shembujt janë në seksionet e ardhshme):

Shembulli i mëposhtëm tregon se si të konvertohet një SavedModel në një model TensorFlow Lite.

import tensorflow as tf

# Convert the model
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) # path to the SavedModel directory
tflite_model = converter.convert()

# Save the model.
with open('model.tflite', 'wb') as f:
  f.write(tflite_model)

Konvertoni një model Keras

Shembulli i mëposhtëm tregon se si të konvertohet një model Keras në një model TensorFlow Lite.

import tensorflow as tf

# Create a model using high-level tf.keras.* APIs
model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(units=1, input_shape=[1]),
    tf.keras.layers.Dense(units=16, activation='relu'),
    tf.keras.layers.Dense(units=1)
])
model.compile(optimizer='sgd', loss='mean_squared_error') # compile the model
model.fit(x=[-1, 0, 1], y=[-3, -1, 1], epochs=5) # train the model
# (to generate a SavedModel) tf.saved_model.save(model, "saved_model_keras_dir")

# Convert the model.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

# Save the model.
with open('model.tflite', 'wb') as f:
  f.write(tflite_model)

Konvertoni funksionet konkrete

Shembulli i mëposhtëm tregon se si të konvertohen funksionet konkrete në një model LiteRT.

import tensorflow as tf

# Create a model using low-level tf.* APIs
class Squared(tf.Module):
  @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.float32)])
  def __call__(self, x):
    return tf.square(x)
model = Squared()
# (ro run your model) result = Squared(5.0) # This prints "25.0"
# (to generate a SavedModel) tf.saved_model.save(model, "saved_model_tf_dir")
concrete_func = model.__call__.get_concrete_function()

# Convert the model.

converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func],
                                                            model)
tflite_model = converter.convert()

# Save the model.
with open('model.tflite', 'wb') as f:
  f.write(tflite_model)

Karakteristika të tjera

  • Aplikoni optimizimet . Një optimizim i zakonshëm i përdorur është kuantizimi pas trajnimit , i cili mund të zvogëlojë më tej vonesën dhe madhësinë e modelit tuaj me humbje minimale në saktësi.

  • Shtoni meta të dhëna , të cilat e bëjnë më të lehtë krijimin e kodit të mbështjellësit specifik të platformës kur vendosni modele në pajisje.

Gabimet e konvertimit

Më poshtë janë gabimet e zakonshme të konvertimit dhe zgjidhjet e tyre:

Mjeti i Linjës së Komandës

Nëse e keni instaluar TensorFlow 2.x nga pip , përdorni komandën tflite_convert . Për të parë të gjitha flamujt e disponueshëm, përdorni komandën e mëposhtme:

$ tflite_convert --help

`--output_file`. Type: string. Full path of the output file.
`--saved_model_dir`. Type: string. Full path to the SavedModel directory.
`--keras_model_file`. Type: string. Full path to the Keras H5 model file.
`--enable_v1_converter`. Type: bool. (default False) Enables the converter and flags used in TF 1.x instead of TF 2.x.

You are required to provide the `--output_file` flag and either the `--saved_model_dir` or `--keras_model_file` flag.

Nëse e keni shkarkuar burimin TensorFlow 2.x dhe doni ta ekzekutoni konvertuesin nga ai burim pa e ndërtuar dhe instaluar paketën, mund ta zëvendësoni ' tflite_convert ' me ' bazel run tensorflow/lite/python:tflite_convert -- ' në komandë.

Konvertimi i një modeli të ruajtur

tflite_convert \
  --saved_model_dir=/tmp/mobilenet_saved_model \
  --output_file=/tmp/mobilenet.tflite

Konvertimi i një modeli Keras H5

tflite_convert \
  --keras_model_file=/tmp/mobilenet_keras_model.h5 \
  --output_file=/tmp/mobilenet.tflite