我试图在 tensorflow 中使用 VGG16 网络运行 vanilla Image 网络分类(通过 Keras 主干提供 VGG16)。
然而,当我试图对一个大象样本图像进行分类时,它给出了完全出乎意料的结果。
我无法弄清楚可能是什么问题。
这是我使用的完整代码:
import tensorflow as tf
import numpy as np
from PIL import Image
from tensorflow.python.keras._impl.keras.applications import imagenet_utils
model = tf.keras.applications.VGG16()
VGG = model.graph
VGG.get_operations()
input = VGG.get_tensor_by_name("input_1:0")
output = VGG.get_tensor_by_name("predictions/Softmax:0")
print(input)
print(output)
I = Image.open("Elephant.jpg")
new_img = I.resize((224,224))
image_array = np.array(new_img)[:, :, 0:3]
image_array = np.expand_dims(image_array, axis=0)
with tf.Session(graph=VGG) as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
pred = (sess.run(output,{input:image_array}))
print(imagenet_utils.decode_predictions(pred))
以下是我得到的示例输出:
Tensor("input_1:0", shape=(?, 224, 224, 3), dtype=float32)
Tensor("predictions/Softmax:0", shape=(?, 1000), dtype=float32)[[('n02281406', 'sulphur_butterfly', 0.0022673723), ('n01882714', '考拉', 0.0021256246), ('n04325704', '偷走', 0.0020583202), ('n01496331', 021', 40, 026 电. ('n01797886', 'ruffed_grouse', 0.0020229272)]]
从概率上看,传递的图像数据似乎有问题(因为所有数据都非常低)。
但我无法弄清楚出了什么问题。
而且我非常确定这张照片是大象作为人类的形象!