Ich habe ein VGG-Modell geschrieben, und es trainiert.Jetzt werde ich ein neues Bild testen.Wenn ich Code1 verwende, funktioniert es gut, aber es ist falsch in Code2. vgg() ist das Modell, das ich definiert habe. Und die ckpt Datei wurde in "D: \ Demo \ ckpt" gespeichert.Als ich tf.nn.read_file() benutzte, um ein Bild in vgg zu testen, war es falsch. Aber als ich PIL benutzte, um es zu öffnen, war es korrekt. So seltsam
code1: druckt Vorhersage als [[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)
----------------- ------------------------------- Trennlinie ----------------- ---------------------
code2: Ich habe einige Funktionen in Tensorflow.It gedruckte Vorhersage als [[0,33333334 0,333333334 0,333333334]].
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)
Ich denke, die beiden Codes fast gleichen steps.But haben ich weiß nicht, warum es falsch ist, und wie it.Help mich zu handhaben, vielen Dank!
Vielen Dank. – Ericccccc
Ein neues Problem trat auf, als ich "tf.image.per_image_standardization (image_tensor)" verwendete. Hast du irgendwelche Vorschläge? Vielen Dank. – Ericccccc
ich aktualisierte Antwort und fügte Code –