Python 在复制纸质结果时,Keras VGG16的准确性较差
我正试图通过使用Keras和Tensorflow GPU后端,从中复制一些结果,并获得完全不正确的标签 我从那篇文章中截取了图1(a)的屏幕截图,并试图从Keras应用程序中生成经过预训练的VGG16来对其进行分类 这是我的照片: 这是我的代码(Jupyter笔记本上的手机)。部分代码是从 输出:Python 在复制纸质结果时,Keras VGG16的准确性较差,python,tensorflow,keras,Python,Tensorflow,Keras,我正试图通过使用Keras和Tensorflow GPU后端,从中复制一些结果,并获得完全不正确的标签 我从那篇文章中截取了图1(a)的屏幕截图,并试图从Keras应用程序中生成经过预训练的VGG16来对其进行分类 这是我的照片: 这是我的代码(Jupyter笔记本上的手机)。部分代码是从 输出: [[('n03788365', 'mosquito_net', 0.017053505), ('n03291819', 'envelope', 0.015034639), ('n150751
[[('n03788365', 'mosquito_net', 0.017053505),
('n03291819', 'envelope', 0.015034639),
('n15075141', 'toilet_tissue', 0.012603286),
('n01737021', 'water_snake', 0.010620943),
('n04209239', 'shower_curtain', 0.009625845)]]
然而,当我将同样的图片提交给论文作者运行的在线服务时,我看到了正确的标签“Boxer”
这是他们称之为“终端”的东西的输出:
我在Windows7上使用64位AnacondaPython
我的电脑上相关软件的版本:
keras 2.2.2 0
keras-applications 1.0.4 py36_1
keras-base 2.2.2 py36_0
keras-preprocessing 1.0.2 py36_1
tensorflow 1.10.0 eigen_py36h849fbd8_0
tensorflow-base 1.10.0 eigen_py36h45df0d8_0
我做错了什么?如何获得boxer标签?您显然无法执行以下操作
dog_img = dog_img[:, :, 0:3] # Opera has added alpha channel
因此,我使用Keras中名为load\u img
的实用程序加载了图像,该实用程序不添加alpha通道
完整代码
import imageio
from matplotlib import pyplot as plt
from skimage.transform import resize
import numpy as np
from keras import activations
from keras.applications import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input, decode_predictions
# Build the VGG16 network with ImageNet weights
model = VGG16(weights='imagenet', include_top=True)
dog_img = image.img_to_array(image.load_img(r"F:\tmp\Opera Snapshot_2018-09-24_133452_arxiv.org.png", target_size=(224, 224)))
x = np.expand_dims(dog_img, axis=0)
x = preprocess_input(x)
pred = model.predict(x)
print(decode_predictions(pred))
[[('n02108089', 'boxer', 0.29122102), ('n02108422', 'bull_mastiff', 0.199128), ('n02129604', 'tiger', 0.10050287), ('n02123159', 'tiger_cat', 0.09733449), ('n02109047', 'Great_Dane', 0.056869864)]]
显然你不能做下面这行
dog_img = dog_img[:, :, 0:3] # Opera has added alpha channel
因此,我使用Keras中名为load\u img
的实用程序加载了图像,该实用程序不添加alpha通道
完整代码
import imageio
from matplotlib import pyplot as plt
from skimage.transform import resize
import numpy as np
from keras import activations
from keras.applications import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input, decode_predictions
# Build the VGG16 network with ImageNet weights
model = VGG16(weights='imagenet', include_top=True)
dog_img = image.img_to_array(image.load_img(r"F:\tmp\Opera Snapshot_2018-09-24_133452_arxiv.org.png", target_size=(224, 224)))
x = np.expand_dims(dog_img, axis=0)
x = preprocess_input(x)
pred = model.predict(x)
print(decode_predictions(pred))
[[('n02108089', 'boxer', 0.29122102), ('n02108422', 'bull_mastiff', 0.199128), ('n02129604', 'tiger', 0.10050287), ('n02123159', 'tiger_cat', 0.09733449), ('n02109047', 'Great_Dane', 0.056869864)]]
考虑到所有的输出概率都非常低,并且大致平均分布在0.01左右,我的猜测是您对图像进行了错误的预处理,并将某种看起来像噪声的加扰图像传递给
model.predict()
。试着调试并imshow
图像,然后再predict()()
。试着调试并imshow
图像,然后再predict()
调用imshow存在,我看到输出中有狗和猫的图像,明白了吗,所以问题出在256范围内?我曾经遇到过类似的问题,当我不小心从一个应用程序加载解码预测并将其应用到另一个应用程序时(我想我从densenet获得了解码预测并应用到Exception)imshow调用存在,我看到输出中有狗和猫的图像,明白了吗,所以问题出在256范围内?我曾经遇到过类似的问题,当我不小心从一个应用程序加载解码预测并应用到另一个应用程序时(我想我从densenet获得了解码预测,并应用到Exception)像素值的范围必须为0..255,而imageio.imread
返回0..1。乘以256已解决问题像素值的范围必须为0..255,而imageio.imread
返回0..1。乘以256已解决问题