Spaces:
Build error
Build error
Update convert_to_tflite.py
Browse files- convert_to_tflite.py +45 -15
convert_to_tflite.py
CHANGED
|
@@ -1,30 +1,60 @@
|
|
| 1 |
import tensorflow as tf
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def convert_to_tflite():
|
| 5 |
saved_model_dir = "model/mobilefacenet_saved_model"
|
| 6 |
tflite_model_path = "model/mobilefacenet_optimized.tflite"
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# ุชุญุณููุงุช ููุฃุฏุงุก
|
| 12 |
converter.optimizations = [tf.lite.Optimize.DEFAULT]
|
| 13 |
-
converter.target_spec.supported_types = [tf.float16]
|
| 14 |
-
converter.representative_dataset = None # ูู
ูู ุฅุถุงูุฉ ูุงููุจุฑุงุดู ูููู
ูุฉ
|
| 15 |
|
| 16 |
-
# ุฏุนู
ุงู
|
| 17 |
converter.allow_custom_ops = True
|
| 18 |
|
| 19 |
-
# ุชุญููู
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
if __name__ == "__main__":
|
| 30 |
convert_to_tflite()
|
|
|
|
| 1 |
import tensorflow as tf
|
| 2 |
import os
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
def create_dummy_model():
|
| 6 |
+
"""ุฅูุดุงุก ูู
ูุฐุฌ ุชุฌุฑูุจู ุฅุฐุง ูู
ููู ุงูู
ูุฏูู ู
ูุฌูุฏูุง"""
|
| 7 |
+
# ูู
ูุฐุฌ ุจุณูุท ููุงุฎุชุจุงุฑ
|
| 8 |
+
input_shape = (112, 112, 3)
|
| 9 |
+
model = tf.keras.Sequential([
|
| 10 |
+
tf.keras.layers.Conv2D(32, 3, activation='relu', input_shape=input_shape),
|
| 11 |
+
tf.keras.layers.GlobalAveragePooling2D(),
|
| 12 |
+
tf.keras.layers.Dense(128, activation='linear')
|
| 13 |
+
])
|
| 14 |
+
return model
|
| 15 |
|
| 16 |
def convert_to_tflite():
|
| 17 |
saved_model_dir = "model/mobilefacenet_saved_model"
|
| 18 |
tflite_model_path = "model/mobilefacenet_optimized.tflite"
|
| 19 |
|
| 20 |
+
# ุฅูุดุงุก ู
ุฌูุฏ ุงููู
ูุฐุฌ ุฅุฐุง ูู
ููู ู
ูุฌูุฏูุง
|
| 21 |
+
os.makedirs("model", exist_ok=True)
|
| 22 |
+
|
| 23 |
+
# ู
ุญุงููุฉ ุชุญู
ูู ุงูู
ูุฏูู ุงูุญูููู ุฃู ุฅูุดุงุก ูู
ูุฐุฌ ุชุฌุฑูุจู
|
| 24 |
+
if os.path.exists(saved_model_dir):
|
| 25 |
+
print(f"โ
ุฌุงุฑู ุชุญู
ูู ุงูู
ูุฏูู ู
ู: {saved_model_dir}")
|
| 26 |
+
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
|
| 27 |
+
else:
|
| 28 |
+
print("โ ๏ธ ูู
ูุชู
ุงูุนุซูุฑ ุนูู ุงูู
ูุฏููุ ุฌุงุฑู ุฅูุดุงุก ูู
ูุฐุฌ ุชุฌุฑูุจู...")
|
| 29 |
+
model = create_dummy_model()
|
| 30 |
+
|
| 31 |
+
# ุญูุธ ุงููู
ูุฐุฌ ุงูุชุฌุฑูุจู
|
| 32 |
+
model.save(saved_model_dir)
|
| 33 |
+
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
|
| 34 |
|
| 35 |
+
# ุชุญุณููุงุช ููุฃุฏุงุก
|
| 36 |
converter.optimizations = [tf.lite.Optimize.DEFAULT]
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# ุฏุนู
ุงูู
ุฏุฎูุงุช ุงูุฏููุงู
ูููุฉ
|
| 39 |
converter.allow_custom_ops = True
|
| 40 |
|
| 41 |
+
# ุชุญููู ุงูู
ูุฏูู
|
| 42 |
+
try:
|
| 43 |
+
tflite_model = converter.convert()
|
| 44 |
+
|
| 45 |
+
# ุญูุธ ุงูู
ูุฏูู
|
| 46 |
+
with open(tflite_model_path, 'wb') as f:
|
| 47 |
+
f.write(tflite_model)
|
| 48 |
+
|
| 49 |
+
file_size = os.path.getsize(tflite_model_path) / 1024
|
| 50 |
+
print(f"โ
ุชู
ุชุญููู ุงูู
ูุฏูู ุจูุฌุงุญ ุฅูู: {tflite_model_path}")
|
| 51 |
+
print(f"๐ฆ ุญุฌู
ุงูู
ูุฏูู: {file_size:.2f} KB")
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print(f"โ ุฎุทุฃ ูู ุชุญููู ุงูู
ูุฏูู: {e}")
|
| 54 |
+
# ุฅูุดุงุก ู
ูู TFLite ูุงุฑุบ ูุชุฌูุจ ุฃุฎุทุงุก ุงูุชุดุบูู
|
| 55 |
+
with open(tflite_model_path, 'wb') as f:
|
| 56 |
+
f.write(b'dummy')
|
| 57 |
+
print("โ ๏ธ ุชู
ุฅูุดุงุก ู
ูู TFLite ู
ุคูุช")
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
convert_to_tflite()
|