【问题标题】:False prediction from efficientnet transfer learning来自高效网络迁移学习的错误预测
【发布时间】:2021-11-12 18:20:23
【问题描述】:

我是 TensorFlow 迁移学习的新手,我选择 tfhub 来简化查找数据集的过程,但现在我很困惑,因为当我尝试使用来自互联网的图像时,我的模型给出了错误的预测。我使用了没有微调的efficientnet_v2_imagenet1k_b0 特征向量来训练来自https://www.kaggle.com/drgfreeman/rockpaperscissors 的石头剪刀布数据集。我使用图像数据生成器和目录流进行数据处理。

这是我的模特here

这是我的火车结果here

这是我的测试结果here

这是我第二次在 tfhub 中使用迁移学习时得到这样的结果。我想知道为什么会发生这种情况以及如何解决它,这样这个问题就不会再发生了。非常感谢您的帮助,也很抱歉我的英语不好。

【问题讨论】:

  • 你能用文字代替图片来放置代码吗?
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: tensorflow transfer-learning tensorflow-hub false-positive efficientnet


【解决方案1】:

我已将您的代码下载到我的本地计算机和数据集。 必须进行一些调整才能使其在本地运行。 我相信模型efficientnet_v2_imagenet1k_b0 是不同的 来自更新的高效网络模型,因为这个版本确实 要求像素级别在 0 和 1 之间缩放。我运行了模型 有和没有重新缩放,只有当像素 被重新调整。下面是我用来测试模型是否正确预测的代码 从网上下载的一张图片。它按预期工作。

import cv2
class_dict=train_generator.class_indices
print (class_dict)
rev_dict={}
for key, value in class_dict.items():
    rev_dict[value]=key
print (rev_dict)
fpath=r'C:\Temp\rps\1.jpg' # an image downloaded from internet that should be paper class
img=plt.imread(fpath)
print (img.shape)
img=cv2.resize(img, (224,224)) # resize to 224 X 224 to be same size as model was trained on
print (img.shape)
plt.imshow(img)
img=img/255.0 # rescale as was done with training images
img=np.expand_dims(img,axis=0)
print(img.shape)
p=model.predict(img)
print (p)
index=np.argmax(p)
print (index)
klass=rev_dict[index]
prob=p[0][index]* 100
print (f'image is of class {klass}, with probability of {prob:6.2f}')

结果

{'paper': 0, 'rock': 1, 'scissors': 2}
{0: 'paper', 1: 'rock', 2: 'scissors'}
(300, 300, 3)
(224, 224, 3)
(1, 224, 224, 3)
[[9.9902594e-01 5.5121275e-04 4.2284720e-04]]
0
image is of class paper, with probability of  99.90

你的代码中有这个

uploaded = files.upload()

len_file = len(uploaded.keys())

这没有运行,因为文件没有定义 所以找不到导致您的错误分类问题的原因。 请记住在 flow_from_directory 中,如果您不指定颜色模式,则默认为 rgb。所以即使训练图像是 4 通道 PNG 实际模型在 3 个通道上进行训练。因此,请确保您要预测的图像是 3 个通道。

【讨论】:

    【解决方案2】:

    为了帮助确实需要查看有关如何将数据提供给 model.predict 的代码。然而,作为一种猜测,请记住,efficientnet 需要具有 0 到 255 范围内的像素,因此不要缩放图像。确保您的测试图像是 rgb 并且与训练中使用的图像大小相同。还需要查看如何处理预测的代码

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多