我已经编写了一个vgg模型并对其进行了训练。现在,我将测试一个新的图像。当我使用code1时,它运行良好,但在code2中是错误的。 vgg()是我定义它的模型。并且ckpt文件已保存在“D:\ Demo \ ckpt”中。
code1:打印预测为[[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)
----------------------------------------------- -Dividing Line --------------------------------------
code2:我在tensorflow中使用了一些函数。它打印的预测为[[0.33333334 0.33333334 0.33333334]]。
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)
我认为这两个代码的步骤几乎相同。但我不知道为什么这是错的,以及如何处理它。帮助我,非常感谢你!
答案 0 :(得分:0)
第一个代码调整大小图片。第二个代码裁剪图片。这是两个不同的动作,第一个动作将完整图像缩放到新尺寸,第二个动作切割部分图像以使其大小等于224 x 224
<强>更新强>
试试这段代码:
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.expand_dims(image_tensor, [0])
image_tensor = tf.image.resize_images(image_tensor, [224, 224])
logit = vgg(image_tensor)
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)
print(prediction)
答案 1 :(得分:0)
当我使用此代码时,发生错误。结果再次为[[0.33333334 0.33333334 0.33333334]。
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.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)