หน้านี้อธิบายวิธีแปลงโมเดล TensorFlow เป็นโมเดล TensorFlow Lite (รูปแบบ FlatBuffer ที่ปรับให้เหมาะสมซึ่งระบุโดยนามสกุลไฟล์ .tflite
) โดยใช้ตัวแปลง TensorFlow Lite
เวิร์กโฟลว์การแปลง
ไดอะแกรมด้านล่างแสดงเวิร์กโฟลว์ระดับสูงสำหรับการแปลงแบบจำลองของคุณ:
รูปที่ 1 เวิร์กโฟลว์ตัวแปลง
คุณสามารถแปลงแบบจำลองของคุณโดยใช้หนึ่งในตัวเลือกต่อไปนี้:
- Python API ( แนะนำ ): สิ่งนี้ช่วยให้คุณรวมการแปลงเข้ากับไปป์ไลน์การพัฒนาของคุณ ใช้การปรับให้เหมาะสม เพิ่มข้อมูลเมตา และงานอื่น ๆ อีกมากมายที่ทำให้กระบวนการแปลงง่ายขึ้น
- บรรทัดคำสั่ง : รองรับเฉพาะการแปลงโมเดลพื้นฐานเท่านั้น
Python API
รหัสตัวช่วย: หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับ TensorFlow Lite converter API ให้เรียกใช้ print(help(tf.lite.TFLiteConverter))
แปลงโมเดล TensorFlow โดยใช้ tf.lite.TFLiteConverter
โมเดล TensorFlow ถูกจัดเก็บโดยใช้รูปแบบ SavedModel และสร้างขึ้นโดยใช้ tf.keras.*
API ระดับสูง (โมเดล Keras) หรือ tf.*
API ระดับต่ำ (ซึ่งคุณสร้างฟังก์ชันที่เป็นรูปธรรม) ด้วยเหตุนี้ คุณจึงมีสามตัวเลือกต่อไปนี้ (ตัวอย่างอยู่ในส่วนถัดไป):
-
tf.lite.TFLiteConverter.from_saved_model()
( แนะนำ ): แปลง SavedModel -
tf.lite.TFLiteConverter.from_keras_model()
: แปลงโมเดล Keras -
tf.lite.TFLiteConverter.from_concrete_functions()
: แปลง ฟังก์ชันที่เป็นรูปธรรม
แปลงรูปแบบที่บันทึกไว้ (แนะนำ)
ตัวอย่างต่อไปนี้แสดงวิธีการแปลง SavedModel เป็นโมเดล 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)
แปลงโมเดล Keras
ตัวอย่างต่อไปนี้แสดงวิธีการแปลงโมเดล Keras เป็นโมเดล 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)
แปลงฟังก์ชันคอนกรีต
ตัวอย่างต่อไปนี้แสดงวิธีการแปลง ฟังก์ชันที่เป็นรูปธรรม ให้เป็นแบบจำลอง 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)
คุณสมบัติอื่นๆ
ใช้ การเพิ่มประสิทธิภาพ การปรับให้เหมาะสมทั่วไปที่ใช้คือ การหาปริมาณหลังการฝึก ซึ่งสามารถลดเวลาแฝงและขนาดของโมเดลของคุณได้อีก โดยสูญเสียความแม่นยำน้อยที่สุด
เพิ่ม ข้อมูลเมตา ซึ่งช่วยให้สร้างโค้ด wrapper เฉพาะแพลตฟอร์มได้ง่ายขึ้นเมื่อปรับใช้โมเดลบนอุปกรณ์
ข้อผิดพลาดในการแปลง
ต่อไปนี้เป็นข้อผิดพลาดทั่วไปในการแปลงและวิธีแก้ไข:
ข้อผิดพลาด:
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 ops ที่ไม่มีการใช้งาน TFLite ที่สอดคล้องกัน คุณสามารถแก้ไขได้โดย ใช้ TF op ในรุ่น TFLite (แนะนำ) หากคุณต้องการสร้างโมเดลด้วย TFLite ops เท่านั้น คุณสามารถเพิ่มคำขอสำหรับ TFLite op ที่หายไปใน Github ปัญหา #21526 (แสดงความคิดเห็นหากคำขอของคุณยังไม่ได้กล่าวถึง) หรือ สร้าง TFLite op ด้วยตัวคุณเอง
ข้อผิดพลาด:
.. is neither a custom op nor a flex op
วิธีแก้ไข: หาก TF op นี้คือ:
รองรับใน TF: ข้อผิดพลาดเกิดขึ้นเนื่องจาก TF op หายไปจากรายการที่อนุญาต (รายการของ TF ops ที่ TFLite รองรับ) คุณสามารถแก้ไขปัญหานี้ได้ดังนี้:
ไม่รองรับใน TF: ข้อผิดพลาดเกิดขึ้นเนื่องจาก TFLite ไม่รู้จักตัวดำเนินการ TF แบบกำหนดเองที่คุณกำหนด คุณสามารถแก้ไขปัญหานี้ได้ดังนี้:
- สร้าง TF op
- แปลงโมเดล TF เป็นโมเดล TFLite
- สร้าง TFLite op และเรียกใช้การอนุมานโดยลิงก์กับรันไทม์ TFLite
เครื่องมือบรรทัดคำสั่ง
หากคุณได้ ติดตั้ง 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 เพื่อเรียกใช้การอนุมานบนอุปกรณ์ไคลเอ็นต์ (เช่น อุปกรณ์เคลื่อนที่ แบบฝัง)