【发布时间】:2023-03-19 08:08:01
【问题描述】:
我试图在 tensorflow 中使用 VGG16 网络运行一个香草图像网络分类(通过 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))
以下是我得到的示例输出:
张量("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), ('electric_ray,', 0.0020416214), ('n01797886', 'ruffed_grouse', 0.0020229272)]]
从概率来看,传递的图像数据似乎有问题(因为所有数据都非常低)。
但我不知道出了什么问题。
而且我很确定这张照片是一头大象作为人类!
【问题讨论】:
标签: python tensorflow computer-vision conv-neural-network imagenet