我编写了一个vgg模型,并对其进行了训练。现在,我要测试一个新图像。当我使用code1时,它运行良好,但在code2中是错误的。
vgg()是我定义的模型。ckpt文件已保存在“D:\Demo\ckpt”中。
代码1:它将预测打印为[1.77901700e-01 8.22093844e-01 4.42284863e-06]]
def evaluate_one_image(path):
with tf.Graph().as_default():
image_plt = Image.open(path)
image = image_plt.resize([224, 224])
image_array = np.array(image)
image = np.reshape(image_array, (1,224,224,3))
x = tf.placeholder(tf.float32, shape=[1, 224, 224, 3])
logit = vgg(x)
logit = tf.nn.softmax(logit)
logs_train_dir = 'D:\\Demo\\ckpt'
saver = tf.train.Saver(tf.global_variables())
with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state(logs_train_dir)
saver.restore(sess, ckpt.model_checkpoint_path)
prediction = sess.run(logit, feed_dict={x: image})
print(prediction)
------------------------------------------------分界线--------------------------------------
代码2:我在tensorflow中使用了一些函数。它将预测打印为[0.33333334 0.33333334 0.3333333 4]]。
def test_one_image(path):
with tf.Graph().as_default():
image_plt = Image.open(path)
image_tensor = tf.image.decode_jpeg(tf.read_file(path), channels=3)
image_tensor = tf.image.resize_image_with_crop_or_pad(image_tensor, 224, 224)
# image_tensor = tf.image.per_image_standardization(image_tensor)
image_tensor = tf.reshape(image_tensor, [1, 224, 224, 3])
x = tf.placeholder(tf.float32, shape=[1, 224, 224, 3])
logit = vgg(x)
logit = tf.nn.softmax(logit)
logs_train_dir = 'D:\\Demo\\ckpt'
saver = tf.train.Saver(tf.global_variables())
with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state(logs_train_dir)
saver.restore(sess, ckpt.model_checkpoint_path)
prediction = sess.run(logit, feed_dict={x: image_tensor.eval()})
print(prediction)
我认为这两个代码的步骤几乎相同。但我不知道为什么这是错误的,以及如何处理它。帮帮我,非常感谢!