-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_to_lite_model.py
41 lines (28 loc) · 1.05 KB
/
convert_to_lite_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
import tensorflow as tf
from tensorflow.keras import Input
from src.model.gan import ScreamGAN
from src.config import *
OUTPUT_PATH = os.path.join("weights","lite model","screamer.tflite")
if __name__ == '__main__':
input_shape = (SEGMENT_LENGTH)
warmup_input = Input(shape=input_shape)
gan = ScreamGAN(tflite=True, use_weight_norm=False)
gan(warmup_input)
if os.path.exists(WEIGHTS_PATH + ".index"):
print("Loading GAN's weights...")
gan.load_weights(WEIGHTS_PATH)
print("GAN's weights successfully loaded!")
else:
raise Exception("GAN's weights not found.")
# Convert the model
converter = tf.lite.TFLiteConverter.from_keras_model(gan.generator)
converter.target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.
tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops.
]
tflite_model = converter.convert()
# Save the model.
with open(OUTPUT_PATH, 'wb') as f:
f.write(tflite_model)
print("Conversion completed!")