TensorFlow के मॉडल बदलें

इस पेज पर यह जानकारी दी गई है कि TensorFlow Lite कन्वर्टर का इस्तेमाल करके, TensorFlow मॉडल को TensorFlow Lite मॉडल में कैसे बदला जा सकता है. यह एक ऑप्टिमाइज़ किया गया FlatBuffer फ़ॉर्मैट है, जिसकी पहचान .tflite फ़ाइल एक्सटेंशन से करता है.

कन्वर्ज़न का वर्कफ़्लो

नीचे दिए गए डायग्राम में, आपके मॉडल को बदलने के लिए हाई-लेवल वर्कफ़्लो दिखाया गया है:

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

पहला डायग्राम. कन्वर्टर का वर्कफ़्लो.

नीचे दिए गए विकल्पों में से किसी एक का इस्तेमाल करके, मॉडल को बदला जा सकता है:

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

Python एपीआई

हेल्पर कोड: TensorFlow Lite कन्वर्टर एपीआई के बारे में ज़्यादा जानने के लिए, print(help(tf.lite.TFLiteConverter)) चलाएं.

tf.lite.TFLiteConverter का इस्तेमाल करके, TensorFlow के मॉडल को बदलें. TensorFlow के मॉडल को सेव किए गए मॉडल फ़ॉर्मैट का इस्तेमाल करके सेव किया जाता है. इसे हाई लेवल tf.keras.* एपीआई (एक Keras मॉडल) या लो-लेवल tf.* एपीआई (इससे कंक्रीट फ़ंक्शन जनरेट करने) का इस्तेमाल करके जनरेट किया जाता है. इस वजह से, आपके पास ये तीन विकल्प हैं (उदाहरण अगले कुछ सेक्शन में दिए गए हैं):

नीचे दिए गए उदाहरण में, SavedModel को TensorFlow लाइट मॉडल में बदलने का तरीका बताया गया है.

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)

Keras मॉडल को बदलना

नीचे दिए गए उदाहरण में, Keras मॉडल को TensorFlow लाइट मॉडल में बदलने का तरीका दिखाया गया है.

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)

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

कन्वर्ज़न से जुड़ी गड़बड़ियां

कन्वर्ज़न से जुड़ी सामान्य गड़बड़ियां और उनके समाधान यहां दिए गए हैं:

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

अगर आपने पीआईपी से 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

Keras H5 मॉडल को बदलना

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