यह पृष्ठ वर्णन करता है कि TensorFlow Lite कनवर्टर का उपयोग करके TensorFlow मॉडल को TensorFlow Lite मॉडल ( .tflite
फ़ाइल एक्सटेंशन द्वारा पहचाना गया एक अनुकूलित फ़्लैटबफ़र प्रारूप) में कैसे परिवर्तित किया जाए।
रूपांतरण कार्यप्रवाह
नीचे दिया गया चित्र आपके मॉडल को परिवर्तित करने के लिए उच्च-स्तरीय वर्कफ़्लो को दर्शाता है:
चित्र 1. कनवर्टर वर्कफ़्लो.
आप निम्न विकल्पों में से किसी एक का उपयोग करके अपना मॉडल परिवर्तित कर सकते हैं:
- पायथन एपीआई ( अनुशंसित ): यह आपको रूपांतरण को अपनी विकास पाइपलाइन में एकीकृत करने, अनुकूलन लागू करने, मेटाडेटा जोड़ने और कई अन्य कार्यों को जोड़ने की अनुमति देता है जो रूपांतरण प्रक्रिया को सरल बनाते हैं।
- कमांड लाइन : यह केवल मूल मॉडल रूपांतरण का समर्थन करता है।
पायथन एपीआई
हेल्पर कोड: TensorFlow Lite कनवर्टर एपीआई के बारे में अधिक जानने के लिए, print(help(tf.lite.TFLiteConverter))
चलाएँ।
tf.lite.TFLiteConverter
का उपयोग करके TensorFlow मॉडल कनवर्ट करें। एक TensorFlow मॉडल को SavedModel प्रारूप का उपयोग करके संग्रहीत किया जाता है और इसे या तो उच्च-स्तरीय tf.keras.*
API (एक Keras मॉडल) या निम्न-स्तरीय tf.*
API (जिससे आप ठोस फ़ंक्शन उत्पन्न करते हैं) का उपयोग करके उत्पन्न किया जाता है। परिणामस्वरूप, आपके पास निम्नलिखित तीन विकल्प हैं (उदाहरण अगले कुछ अनुभागों में हैं):
-
tf.lite.TFLiteConverter.from_saved_model()
( अनुशंसित ): एक SavedModel को रूपांतरित करता है। -
tf.lite.TFLiteConverter.from_keras_model()
: केरस मॉडल को परिवर्तित करता है। -
tf.lite.TFLiteConverter.from_concrete_functions()
: ठोस कार्यों को परिवर्तित करता है।
एक सेव्डमॉडल कनवर्ट करें (अनुशंसित)
निम्नलिखित उदाहरण दिखाता है कि SaveModel को 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)
एक केरस मॉडल परिवर्तित करें
निम्नलिखित उदाहरण दिखाता है कि केरस मॉडल को टेन्सरफ्लो लाइट मॉडल में कैसे परिवर्तित किया जाए।
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 Lite मॉडल में कैसे परिवर्तित किया जाए।
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 कार्यान्वयन नहीं है। आप टीएफलाइट मॉडल (अनुशंसित) में टीएफ ऑप का उपयोग करके इसे हल कर सकते हैं। यदि आप केवल टीएफलाइट ऑप्स के साथ एक मॉडल तैयार करना चाहते हैं, तो आप या तो जीथब अंक #21526 में लापता टीएफलाइट ऑप के लिए एक अनुरोध जोड़ सकते हैं (यदि आपके अनुरोध का पहले से उल्लेख नहीं किया गया है तो एक टिप्पणी छोड़ें) या स्वयं टीएफलाइट ऑप बना सकते हैं ।
त्रुटि:
.. is neither a custom op nor a flex op
समाधान: यदि यह टीएफ ऑप है:
टीएफ में समर्थित: त्रुटि इसलिए होती है क्योंकि टीएफ ऑप अनुमति सूची (टीएफलाइट द्वारा समर्थित टीएफ ऑप्स की एक विस्तृत सूची) से गायब है। आप इसे इस प्रकार हल कर सकते हैं:
टीएफ में असमर्थित: त्रुटि इसलिए होती है क्योंकि टीएफलाइट आपके द्वारा परिभाषित कस्टम टीएफ ऑपरेटर से अनजान है। आप इसे इस प्रकार हल कर सकते हैं:
- टीएफ ऑप बनाएं ।
- TF मॉडल को TFLite मॉडल में बदलें ।
- TFLite ऑप बनाएं और इसे TFLite रनटाइम से जोड़कर अनुमान चलाएँ।
कमांड लाइन टूल
यदि आपने pip से TensorFlow 2.x स्थापित किया है , तो 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
केरस H5 मॉडल को परिवर्तित करना
tflite_convert \
--keras_model_file=/tmp/mobilenet_keras_model.h5 \
--output_file=/tmp/mobilenet.tflite
अगले कदम
क्लाइंट डिवाइस (उदाहरण के लिए मोबाइल, एम्बेडेड) पर अनुमान चलाने के लिए TensorFlow Lite दुभाषिया का उपयोग करें।