session.run()对于相同的输入给出不同的结果

问题描述 投票:0回答:1

我正在加载一个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
1个回答
0
投票

我当然没有使用正确的方法重新加载TF21保存的模型。

© www.soinside.com 2019 - 2024. All rights reserved.