我已将您的代码下载到我的本地计算机和数据集。
必须进行一些调整才能使其在本地运行。
我相信模型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 个通道。