首页 > 解决方案 > tensorflow2加载pb文件预测的问题

问题描述

目前,我正在使用 tf2.3 和 windows 10 来学习连体网络。训练处理没有问题。我使用model.fitmodel.save将模型保存为 pb 格式。

然后,我使用tf.keras.models.load_model("model_pb",custom_objects={'tf': tf})加载模型,然后是model.predict

可以打印出 model.summary() 信息,但会显示消息:UserWarning : model is not loaded, but a Lambda layer uses it。它可能会导致错误。即使使用不同的图像对,预测结果也仅显示 0.5,这可能是由于模型加载不成功引起的(我猜)。

那么,您能告诉我如何使用 lambda 层正确加载 pb 模型吗?或者我做错了什么?

提前致谢。

这是我的模型保存代码

    '''training and validation code end here'''
model.fit(get_batch(batch_size),
              validation_data = nway_one_shot(model, n_way, n_val),
              epochs = epochs
        )


model.save("/face_recognizer/model_pb")

这是我的 simple_onepair_test.py 代码

import tensorflow as tf
import tensorflow.keras

new_model = tf.keras.models.load_model("model_pb",custom_objects={'tf': tf})
new_model.summary()



imagepath1 = "/face_recognizer/train/s3/1.jpg"
imagepath2 = "/face_recognizer/train/s3/2.jpg"
imagepath = "/face_recognizer/train/s44"

'''
turn the input1 image into tensor
'''
image1 = tf.io.read_file(imagepath1)
image_tensor1 = tf.image.decode_jpeg(image1,3)
image_tensor1 = tf.image.resize(image_tensor1,[100,100])
image_tensor1= image_tensor1/255
image_tensor1 = tf.cast(image_tensor1,dtype = tf.float32)

'''
#turn the input2 image into tensor
'''
image2 = tf.io.read_file(imagepath2)
image_tensor2 = tf.image.decode_jpeg(image2,3)
image_tensor2 = tf.image.resize(image_tensor2,[100,100])
image_tensor2 = image_tensor2/255
image_tensor2 = tf.cast(image_tensor2,dtype = tf.float32)

'''
#expand dims for inputs
'''
image1=tf.expand_dims(image_tensor1,axis=0)
image2 = tf.expand_dims(image_tensor2,axis=0)
temp=[]
temp.append(image1)
temp.append(image2)
'''
#predict the image
'''
result = new_model.predict(temp, verbose=1)
print(result)

这是打印的信息

UserWarning: model is not loaded, but a Lambda layer uses it. It may cause errors.
 

 , UserWarning)
Model: "functional_1"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
input_1 (InputLayer)            [(None, 100, 100, 3) 0
__________________________________________________________________________________________________
input_2 (InputLayer)            [(None, 100, 100, 3) 0
__________________________________________________________________________________________________
sequential (Sequential)         (None, 512)          17349824    input_1[0][0]
                                                                 input_2[0][0]
__________________________________________________________________________________________________
lambda (Lambda)                 (None, 512)          0           sequential[0][0]
                                                                 sequential[1][0]
__________________________________________________________________________________________________
dense_2 (Dense)                 (None, 2048)         1050624     lambda[0][0]
__________________________________________________________________________________________________
dense_3 (Dense)                 (None, 1024)         2098176     dense_2[0][0]
__________________________________________________________________________________________________
dense_4 (Dense)                 (None, 512)          524800      dense_3[0][0]
__________________________________________________________________________________________________
dense_5 (Dense)                 (None, 1)            513         dense_4[0][0]
==================================================================================================
Total params: 21,023,937
Trainable params: 21,015,489
Non-trainable params: 8,448
__________________________________________________________________________________________________

1/1 [==============================] - 0s 2ms/step
[[0.5]]

标签: pythontensorflowtensorflow2.0

解决方案


推荐阅读