作者您好!
我目前已经训练好模型,并成功运行predict.py
,但是您的代码保存的是.h5
模型,我想把他转换成.tflite
模型,于是我自己写了一个转换脚本(freeze.py
):
import tensorflow as tf
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.utils import CustomObjectScope
def relu6(x):
return K.relu(x, max_value=6)
with CustomObjectScope({'relu6': relu6}):
converter = tf.lite.TFLiteConverter.from_keras_model_file("logs/ep010-loss0.387-val_loss0.758.h5")
tflite_model = converter.convert()
open("./facenet_keras.tflite", "wb").write(tflite_model)
运行此脚本报错,报错信息如下:
Traceback (most recent call last):
File "freeze.py", line 42, in <module>
converter = tf.lite.TFLiteConverter.from_keras_model_file("logs/ep010-loss0.387-val_loss0.758.h5")
File "E:\Anaconda3\envs\facenet-keras\lib\site-packages\tensorflow\lite\python\lite.py", line 370, in from_keras_model_file
keras_model = _keras.models.load_model(model_file)
File "E:\Anaconda3\envs\facenet-keras\lib\site-packages\tensorflow\python\keras\engine\saving.py", line 232, in load_model
raise ValueError('No model found in config file.')
ValueError: No model found in config file.
经过我一番百度之后,发现在代码里边,是只保存了模型的权重,原来代码如下:
checkpoint = ModelCheckpoint(os.path.join(save_dir, "ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5"), monitor = 'val_loss', save_weights_only = True, save_best_only = False, period = save_period)
而from_keras_model_file
这个函数会调用load_model
这个函数,所以载入只有权重的模型会报错,于是我作出了以下修改,将 save_weights_only = True
改为 save_weights_only = False
:
checkpoint = ModelCheckpoint(os.path.join(save_dir, "ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5"), monitor = 'val_loss', save_weights_only = False, save_best_only = False, period = save_period)
重新训练了后,得到了若干.h5
模型,我再次运行此脚本,得到的报错如下:
Traceback (most recent call last):
File "freeze.py", line 42, in <module>
converter = tf.lite.TFLiteConverter.from_keras_model_file("logs/ep009-loss0.231-val_loss0.473.h5")
File "E:\Anaconda3\envs\facenet-keras\lib\site-packages\tensorflow\lite\python\lite.py", line 370, in from_keras_model_file
keras_model = _keras.models.load_model(model_file)
File "E:\Anaconda3\envs\facenet-keras\lib\site-packages\tensorflow\python\keras\engine\saving.py", line 266, in load_model
sample_weight_mode=sample_weight_mode)
File "E:\Anaconda3\envs\facenet-keras\lib\site-packages\tensorflow\python\training\checkpointable\base.py", line 442, in _method_wrapper
method(self, *args, **kwargs)
File "E:\Anaconda3\envs\facenet-keras\lib\site-packages\tensorflow\python\keras\engine\training.py", line 273, in compile
loss_functions.append(training_utils.get_loss_function(loss.get(name)))
File "E:\Anaconda3\envs\facenet-keras\lib\site-packages\tensorflow\python\keras\engine\training_utils.py", line 873, in get_loss_function
return losses.get(loss)
File "E:\Anaconda3\envs\facenet-keras\lib\site-packages\tensorflow\python\keras\losses.py", line 594, in get
return deserialize(identifier)
File "E:\Anaconda3\envs\facenet-keras\lib\site-packages\tensorflow\python\keras\losses.py", line 585, in deserialize
printable_module_name='loss function')
File "E:\Anaconda3\envs\facenet-keras\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 212, in deserialize_keras_object
function_name)
ValueError: Unknown loss function:_triplet_loss
这个报错说是找到损失函数。于是我尝试在脚本里边先load_model,再将这个model传给from_keras_model_file,代码如下:
import tensorflow as tf
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.utils import CustomObjectScope
from keras.models import load_model
from nets.facenet_training import triplet_loss
def relu6(x):
return K.relu(x, max_value=6)
with CustomObjectScope({'relu6': relu6}):
model = load_model("logs/ep009-loss0.231-val_loss0.473.h5",custom_objects={'relu6': relu6,'triplet_loss':triplet_loss})
converter = tf.lite.TFLiteConverter.from_keras_model_file(model)
tflite_model = converter.convert()
open("./facenet_keras.tflite", "wb").write(tflite_model)
得到的报错依然是这个:
Traceback (most recent call last):
File "freeze.py", line 45, in <module>
model = load_model("logs/ep009-loss0.231-val_loss0.473.h5",custom_objects={'relu6': relu6,'triplet_loss':triplet_loss})
File "E:\Anaconda3\envs\facenet-keras\lib\site-packages\keras\models.py", line 274, in load_model
sample_weight_mode=sample_weight_mode)
File "E:\Anaconda3\envs\facenet-keras\lib\site-packages\keras\engine\training.py", line 626, in compile
loss_functions.append(losses.get(loss.get(name)))
File "E:\Anaconda3\envs\facenet-keras\lib\site-packages\keras\losses.py", line 122, in get
return deserialize(identifier)
File "E:\Anaconda3\envs\facenet-keras\lib\site-packages\keras\losses.py", line 114, in deserialize
printable_module_name='loss function')
File "E:\Anaconda3\envs\facenet-keras\lib\site-packages\keras\utils\generic_utils.py", line 164, in deserialize_keras_object
':' + function_name)
ValueError: Unknown loss function:_triplet_loss
希望作者能指点一下迷津,谢谢!