猫狗识别


前言

模型初步搭建成功并训练保存,在训练集上的预测准确率挺高的,但是需要其他不在训练集中的样本来检测才能更好地评估模型。


导入相关包

from keras.models import load_model
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import load_img,img_to_array
from matplotlib import pyplot as plt

加载测试模型

model_cnn = load_model('model_cnn.h5')

加载测试样本并进行预处理

est_datagen = ImageDataGenerator(rescale=1. / 255)
test_set = test_datagen.flow_from_directory('./test_set', target_size=(50,50), batch_size=32, class_mode='binary')

模型预测并计算准确率

test_set_predict = model_cnn.predict_classes(test_set)
accuacry = model_cnn.evaluate_generator(test_set)
# print(accuacry)

可视化预测结果

从本地加载九张图片来进行预测

fig = plt.figure(figsize=(8, 8))
for i in range(1,10):
    image_name = 'result/' + str(i) + '.png'
    img_original = load_img(image_name, target_size=(50, 50))
    img = img_to_array(img_original) / 255
    img = img.reshape(-1, 50, 50, 3)
    result = model_cnn.predict_classes(img)
    plt.subplot(3, 3, i)
    plt.imshow(img_original)
    plt.title('dog' if result == 1 else 'cat')
    pass
plt.show()

总结

由最终的准确率计算可知,模型的表现并不是很好,下面将采用经典的VGG16模型来进行优化处理。

版权声明:本文为CSDN博主「易小顺」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/MTYSYS19990212/article/details/114899747

易小顺

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

目标检测——yolov4损失函数

人工智能学习离不开实践的验证,推荐大家可以多在FlyAI-AI竞赛服务平台多参加训练和竞赛,以此来提升自己的能力。FlyAI是为AI开发者提供数据竞赛并支持GPU离线训练的一站式服务平台。每周免费提供项目开源算法样例

目标检测中的先验框(Anchor)

什么是先验框?
了解过目标检测算法的朋友们肯定知道先验框(Anchor)的概念,那么什么是先验框,为什么要有先验框?若要解释这个问题,首先我们需要了解边界框回归原理。
b