หน้านี้อธิบายวิธีแปลงโมเดล TensorFlow เป็นโมเดล TensorFlow Lite (รูปแบบ FlatBuffer ที่เพิ่มประสิทธิภาพตามนามสกุลไฟล์ .tflite
) โดยใช้ตัวแปลง TensorFlow Lite
เวิร์กโฟลว์ Conversion
แผนภาพด้านล่างแสดงเวิร์กโฟลว์ระดับสูงสำหรับการแปลงโมเดล
รูปที่ 1 เวิร์กโฟลว์ของผู้แปลง
คุณแปลงโมเดลโดยใช้ตัวเลือกใดตัวเลือกหนึ่งต่อไปนี้ได้
- Python API (แนะนำ): ตัวเลือกนี้ช่วยให้คุณผสานรวม Conversion ลงในไปป์ไลน์การพัฒนา ใช้การเพิ่มประสิทธิภาพ เพิ่มข้อมูลเมตา และงานอื่นๆ อีกมากมายที่ทำให้กระบวนการ Conversion ง่ายขึ้น
- บรรทัดคำสั่ง: รองรับเฉพาะการแปลงรูปแบบพื้นฐานเท่านั้น
Python API
รหัสตัวช่วย: หากต้องการดูข้อมูลเพิ่มเติมเกี่ยวกับ API ตัวแปลง TensorFlow Lite ให้เรียกใช้ print(help(tf.lite.TFLiteConverter))
แปลงโมเดล TensorFlow โดยใช้ tf.lite.TFLiteConverter
ระบบจะจัดเก็บโมเดล TensorFlow โดยใช้รูปแบบ SavedModel และสร้างโดยใช้ tf.keras.*
API ระดับสูง (โมเดล Keras) หรือ tf.*
API ระดับต่ำ (จากจุดที่คุณใช้สร้างฟังก์ชันที่เป็นรูปธรรม) ดังนั้น คุณมี 3 ตัวเลือกต่อไปนี้ (ตัวอย่างจะอยู่ใน 2-3 หัวข้อถัดไป)
tf.lite.TFLiteConverter.from_saved_model()
(แนะนำ): แปลง SavedModeltf.lite.TFLiteConverter.from_keras_model()
: แปลงโมเดล Kerastf.lite.TFLiteConverter.from_concrete_functions()
: แปลงฟังก์ชันคอนกรีต
แปลง SavedModel (แนะนำ)
ตัวอย่างต่อไปนี้แสดงวิธีแปลง 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 เฉพาะแพลตฟอร์มได้ง่ายขึ้นเมื่อทำให้โมเดลใช้งานได้ในอุปกรณ์
ข้อผิดพลาดในการแปลง
ข้อผิดพลาดที่พบบ่อยเกี่ยวกับ Conversion และวิธีแก้ไขมีดังนี้
ข้อผิดพลาด:
Some ops are not supported by the native TFLite runtime, you can enable TF kernels fallback using TF Select.
วิธีแก้ไข: ข้อผิดพลาดนี้เกิดขึ้นเนื่องจากโมเดลของคุณมี TF Ops ที่ไม่มีการติดตั้งใช้งาน TFLite ที่สอดคล้องกัน ซึ่งแก้ไขได้โดยใช้ TF ในโมเดล TFLite (แนะนำ) หากคุณต้องการสร้างโมเดลที่มีการดำเนินการ TFLite เท่านั้น คุณสามารถเพิ่มคำขอสำหรับ TFLite op ที่ขาดหายไปใน ปัญหา GitHub #21526 (แสดงความคิดเห็นหากยังไม่ได้กล่าวถึงคำขอ) หรือ สร้างคำขอ TFLite ด้วยตนเอง
ข้อผิดพลาด:
.. is neither a custom op nor a flex op
วิธีแก้ไข: หาก TF นี้มีลักษณะดังนี้
มีการรองรับใน TF: ข้อผิดพลาดนี้เกิดขึ้นเพราะไม่มีการดำเนินการ TF ในรายการที่อนุญาต (รายการการดำเนินการ TF ทั้งหมดที่ TFLite รองรับ) คุณสามารถแก้ไขปัญหานี้ได้ดังนี้
ไม่รองรับใน TF: ข้อผิดพลาดนี้เกิดขึ้นเนื่องจาก TFLite ไม่ทราบถึงโอเปอเรเตอร์ TF ที่กำหนดเองซึ่งคุณกำหนด คุณสามารถแก้ไขปัญหานี้ได้ดังนี้
- สร้างการดำเนินการ TF
- แปลงโมเดล 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 --
" ในคำสั่ง
การแปลง SavedModel
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