【发布时间】:2020-05-24 11:10:56
【问题描述】:
我正在加载一个 tensorflow 保存的模型并尝试使用加载的模型进行推断。
以灰度图为输入,输出相同大小的灰度图。
import tensorflow as tf
import numpy as np
from PIL import Image
img_width=224
img_height=224
export_path='/path/to/my/saved/model'
with tf.compat.v1.Session(graph=tf.compat.v1.Graph()) as sess:
# Load model
tf.compat.v1.saved_model.loader.load(sess, ['serve'], export_path)
# Load test image (grayscale)
path = '/path/to/test/images/image.png'
img = Image.open(path)
img.show()
img_data = np.array(img).reshape(1,img_width,img_height,1)
# Infer output
y_pred = sess.run('output:0', feed_dict={'input:0': img_data })
print(y_pred.shape)
# Show output image
im1 = Image.fromarray(np.uint8(y_pred.reshape(img_width,img_height)*255), 'L')
im1.show()
# Infer output
y_pred = sess.run('output:0', feed_dict={'input:0': img_data })
# Show output image
im2 = Image.fromarray(np.uint8(y_pred.reshape(img_width,img_height)*255), 'L')
im2.show()
运行这个简单的代码,我得到了 2 个不同的结果。 Im1 与 Im2 不完全相同。
我做错了吗?
import tensorflow as tf
print(tf.__version__)
2.1.0
【问题讨论】:
标签: python numpy tensorflow tensorflow2.0 tensorflow-serving