แปลง TensorFlow Lite เตะรุ่น TensorFlow และสร้างรูปแบบการ TensorFlow Lite (เพิ่มประสิทธิภาพ FlatBuffer รูปแบบที่ระบุโดย .tflite
นามสกุลไฟล์) คุณมีสองตัวเลือกต่อไปนี้สำหรับการใช้ตัวแปลง:
- งูหลาม API (แนะนำ): นี่ทำให้ง่ายต่อการแปลงรูปแบบที่เป็นส่วนหนึ่งของโครงการพัฒนารูปแบบการใช้เพิ่มประสิทธิภาพเพิ่มข้อมูลเมตาและมีคุณสมบัติอื่น ๆ อีกมากมาย
- บรรทัดคำสั่ง : นี้จะสนับสนุนการแปลงรูปแบบพื้นฐาน
Python API
รหัส Helper: การระบุรุ่น TensorFlow ติดตั้งให้เรียกใช้ print(tf.__version__)
และเพื่อเรียนรู้เพิ่มเติมเกี่ยวกับการแปลง API TensorFlow Lite วิ่ง print(help(tf.lite.TFLiteConverter))
หากคุณได้ ติดตั้ง TensorFlow 2.x คุณมีดังต่อไปนี้สองตัวเลือกนี้ (ถ้าคุณได้ ติดตั้ง TensorFlow 1.x อ้างถึง Github )
แปลงรูปแบบ TensorFlow 2.x ใช้
tf.lite.TFLiteConverter
TensorFlow รุ่น 2.x จะถูกเก็บไว้โดยใช้รูปแบบ SavedModel และถูกสร้างขึ้นทั้งการใช้ในระดับสูงtf.keras.*
APIs (รูปแบบ Keras) หรือระดับต่ำtf.*
APIs (จากการที่คุณสร้างฟังก์ชั่นที่เป็นรูปธรรม) ด้วยเหตุนี้ คุณจึงมีสามตัวเลือกต่อไปนี้ (ตัวอย่างอยู่ในส่วนถัดไป):แปลงรูปแบบ TensorFlow 1.x ใช้
tf.compat.v1.lite.TFLiteConverter
(ตัวอย่างอยู่บน Github ):-
tf.compat.v1.lite.TFLiteConverter.from_saved_model()
: แปลง SavedModel -
tf.compat.v1.lite.TFLiteConverter.from_keras_model_file()
: แปลง Keras รุ่น -
tf.compat.v1.lite.TFLiteConverter.from_session()
: แปลง GraphDef จากการใช้งาน -
tf.compat.v1.lite.TFLiteConverter.from_frozen_graph()
: แปลง GraphDef แช่แข็งจากแฟ้ม ถ้าคุณมีจุดตรวจแล้วครั้งแรกที่แปลงเป็นไฟล์แช่แข็ง GraphDef แล้วใช้ API นี้ตามที่แสดงไว้ ที่นี่
-
แปลง 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.
# Notes that for the versions earlier than TensorFlow 2.7, the
# from_concrete_functions API is able to work when there is only the first
# argument given:
# > converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func])
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 ops ที่ไม่มีการใช้งาน TFLite ที่สอดคล้องกัน คุณสามารถแก้ปัญหานี้โดย ใช้ op TF ในรูปแบบ TFLite (แนะนำ) หากคุณต้องการที่จะสร้างรูปแบบที่มี Ops TFLite เท่านั้นคุณสามารถเพิ่มการร้องขอสำหรับการหายตัวไปของสหกรณ์ TFLite ใน Github รุ่น # 21526 (แสดงความคิดเห็นหากคำขอของคุณยังไม่ได้รับการกล่าวถึงอยู่แล้ว) หรือ สร้างสหกรณ์ TFLite ตัวเอง
ข้อผิดพลาด:
.. is neither a custom op nor a flex op
วิธีแก้ไข: หาก TF op นี้คือ:
การสนับสนุนใน TF: ข้อผิดพลาดที่เกิดขึ้นเนื่องจาก TF สหกรณ์จะหายไปจาก allowlist (รายการครบถ้วนสมบูรณ์ของ Ops TF สนับสนุนโดย TFLite) คุณสามารถแก้ไขปัญหานี้ได้ดังนี้:
ไม่รองรับใน TF: ข้อผิดพลาดเกิดขึ้นเนื่องจาก TFLite ไม่รู้จักตัวดำเนินการ TF แบบกำหนดเองที่คุณกำหนด คุณสามารถแก้ไขปัญหานี้ได้ดังนี้:
- สร้างสหกรณ์ลุย
- แปลงรุ่น TF ไปยังรุ่น TFLite
- สร้างสหกรณ์ TFLite และเรียกใช้การอนุมานโดยการเชื่อมโยงไปยังรันไทม์ TFLite
เครื่องมือบรรทัดคำสั่ง
ก็ขอแนะนำให้คุณใช้ API หลาม ระบุไว้ข้างต้นแทนถ้าเป็นไปได้
หากคุณได้ ติดตั้ง TensorFlow 2.x จาก pip ใช้ tflite_convert
คำสั่งดังต่อไปนี้ (ถ้าคุณได้ ติดตั้ง 2.x TensorFlow จากแหล่ง แล้วคุณสามารถแทนที่ ' tflite_convert
' กับ ' bazel run //tensorflow/lite/python:tflite_convert --
'ในส่วนต่อไปและถ้าคุณได้ ติดตั้ง TensorFlow 1.x แล้วอ้างถึง Github ( อ้างอิง , ตัวอย่าง ))
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.
การแปลงรูปแบบที่บันทึกไว้
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 วิ่งอนุมานบนอุปกรณ์ของลูกค้า (เช่นมือถือฝังตัว)