TensorFlow मॉडल कनवर्ट करें

संग्रह की मदद से व्यवस्थित रहें अपनी प्राथमिकताओं के आधार पर, कॉन्टेंट को सेव करें और कैटगरी में बांटें.

यह पृष्ठ बताता है कि TensorFlow लाइट कनवर्टर का उपयोग करके एक TensorFlow मॉडल को एक TensorFlow लाइट मॉडल ( .tflite फ़ाइल एक्सटेंशन द्वारा पहचाना गया एक अनुकूलित फ़्लैटबफ़र प्रारूप) में कैसे परिवर्तित किया जाए।

रूपांतरण कार्यप्रवाह

नीचे दिया गया आरेख आपके मॉडल को परिवर्तित करने के लिए उच्च-स्तरीय कार्यप्रवाह को दर्शाता है:

TFlite कनवर्टर वर्कफ़्लो

चित्रा 1. कनवर्टर कार्यप्रवाह।

आप निम्न विकल्पों में से किसी एक का उपयोग करके अपने मॉडल को रूपांतरित कर सकते हैं:

  1. पायथन एपीआई ( अनुशंसित ): यह आपको अपनी विकास पाइपलाइन में रूपांतरण को एकीकृत करने, अनुकूलन लागू करने, मेटाडेटा जोड़ने और रूपांतरण प्रक्रिया को सरल बनाने वाले कई अन्य कार्यों की अनुमति देता है।
  2. कमांड लाइन : यह केवल मूल मॉडल रूपांतरण का समर्थन करता है।

पायथन एपीआई

हेल्पर कोड: TensorFlow Lite कन्वर्टर API के बारे में अधिक जानने के लिए, print(help(tf.lite.TFLiteConverter))

tf.lite.TFLiteConverter का उपयोग करके एक tf.lite.TFLiteConverter मॉडल को कनवर्ट करें। एक TensorFlow मॉडल को सहेजे गए मॉडल प्रारूप का उपयोग करके संग्रहीत किया जाता है और या तो उच्च-स्तरीय tf.keras.* API (एक केरस मॉडल) या निम्न-स्तरीय tf.* API (जिससे आप ठोस कार्य उत्पन्न करते हैं) का उपयोग करके उत्पन्न किया जाता है। परिणामस्वरूप, आपके पास निम्नलिखित तीन विकल्प हैं (उदाहरण अगले कुछ अनुभागों में हैं):

निम्न उदाहरण दिखाता है कि सहेजे गए मॉडल को टेंसरफ्लो लाइट मॉडल में कैसे परिवर्तित किया जाए।

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)

एक केरस मॉडल कन्वर्ट करें

निम्न उदाहरण दिखाता है कि केरस मॉडल को टेंसरफ्लो लाइट मॉडल में कैसे परिवर्तित किया जाए।

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)

ठोस कार्यों को परिवर्तित करें

निम्न उदाहरण दिखाता है कि कंक्रीट फ़ंक्शंस को TensorFlow लाइट मॉडल में कैसे परिवर्तित किया जाए।

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)

अन्य सुविधाओं

  • अनुकूलन लागू करें। उपयोग किया जाने वाला एक सामान्य अनुकूलन प्रशिक्षण के बाद का परिमाणीकरण है, जो सटीकता में न्यूनतम नुकसान के साथ आपके मॉडल की विलंबता और आकार को और कम कर सकता है।

  • मेटाडेटा जोड़ें, जिससे उपकरणों पर मॉडल परिनियोजित करते समय प्लेटफ़ॉर्म विशिष्ट रैपर कोड बनाना आसान हो जाता है।

रूपांतरण त्रुटियां

सामान्य रूपांतरण त्रुटियाँ और उनके समाधान निम्नलिखित हैं:

  • त्रुटि: Some ops are not supported by the native TFLite runtime, you can enable TF kernels fallback using TF Select. See instructions: <a href="https://www.tensorflow.org/lite/guide/ops_select">https://www.tensorflow.org/lite/guide/ops_select</a> TF Select ops: ..., .., ...

    समाधान: त्रुटि तब होती है जब आपके मॉडल में TF ऑप्स होते हैं जिनके पास संबंधित TFLite कार्यान्वयन नहीं होता है। आप इसे TFLite मॉडल (अनुशंसित) में TF op का उपयोग करके हल कर सकते हैं। यदि आप केवल TFLite ऑप्स के साथ एक मॉडल बनाना चाहते हैं, तो आप या तो Github अंक # 21526 में लापता TFLite सेशन के लिए एक अनुरोध जोड़ सकते हैं (यदि आपके अनुरोध का पहले से उल्लेख नहीं किया गया है तो एक टिप्पणी छोड़ दें) या स्वयं TFLite ऑप बना सकते हैं।

  • त्रुटि: .. is neither a custom op nor a flex op

    समाधान: यदि यह TF सेशन है:

कमांड लाइन टूल

यदि आपने TensorFlow 2.x को pip से स्थापित किया है, तो tflite_convert कमांड का उपयोग करें। सभी उपलब्ध झंडे देखने के लिए, निम्न आदेश का उपयोग करें:

$ 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.

यदि आपके पास TensorFlow 2.x स्रोत डोनलोड किया गया है और आप उस स्रोत से कनवर्टर को बिना पैकेज बनाए और इंस्टॉल किए चलाना चाहते हैं, तो आप कमांड में ' tflite_convert ' को ' bazel run tensorflow/lite/python:tflite_convert -- ' से बदल सकते हैं।

एक सहेजे गए मॉडल को परिवर्तित करना

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

एक Keras H5 मॉडल परिवर्तित करना

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

अगले कदम

क्लाइंट डिवाइस (जैसे मोबाइल, एम्बेडेड) पर अनुमान चलाने के लिए TensorFlow Lite दुभाषिया का उपयोग करें।