|
地板
楼主 |
发表于 2024-2-28 11:01:26
|
只看该作者
本帖最后由 rockchipshbjack 于 2024-2-28 11:45 编辑
使用以下代码将h5转为tflite
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.python.ops import math_ops
from tensorflow.python.keras import backend as K
#自定义损失
def ReprojectionLoss(y_true, y_pred):
y_pred = ops.convert_to_tensor_v2(y_pred)
y_true = math_ops.cast(y_true, y_pred.dtype)
y_pred = K.reshape(y_pred,(-1,27,2))
y_true = K.reshape(y_true, (-1, 27, 2))
return K.sqrt(K.mean(K.sum(math_ops.squared_difference(y_pred, y_true),axis=-1),axis=-1))
def ReprojectionMetrics(y_true,y_pred):
return ReprojectionLoss(y_true, y_pred)
if __name__ == "__main__":
# 将h5模型转化为tflite模型方法1
modelparh = './mobilenet.h5'
model = tf.keras.models.load_model(modelparh, custom_objects = {'ReprojectionLoss': ReprojectionLoss, 'ReprojectionMetrics': ReprojectionMetrics})
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
savepath = './mobilenet.tflite'
open(savepath, "wb").write(tflite_model)
然后用tflite的接口进行尝试,发现结果变为-----TOP 5-----
[155]: 0.66455078125
[154]: 0.25634765625
[283]: 0.02154541015625
[204 254]: 0.0178070068359375
[204 254]: 0.0178070068359375
感觉模型应该是没问题的,为什么h5就不行呢?
|
|