当前位置: 首页 > news >正文

python与深度学习(十二):CNN和猫狗大战二

目录

  • 1. 说明
  • 2. 猫狗大战的CNN模型测试
    • 2.1 导入相关库
    • 2.2 加载模型
    • 2.3 设置保存图片的路径
    • 2.4 加载图片
    • 2.5 图片预处理
    • 2.6 对图片进行预测
    • 2.7 显示图片
  • 3. 完整代码和显示结果
  • 4. 多张图片进行测试的完整代码以及结果

1. 说明

本篇文章是对上篇文章猫狗大战训练的模型进行测试。首先是将训练好的模型进行重新加载,然后采用opencv对图片进行加载,最后将加载好的图片输送给模型并且显示结果。

2. 猫狗大战的CNN模型测试

2.1 导入相关库

在这里导入需要的第三方库如cv2,如果没有,则需要自行下载,自行下载时候一般建议镜像源,这样下载的快。

from tensorflow import keras
import skimage, os, sys, cv2
from PIL import ImageFont, Image, ImageDraw  # PIL就是pillow包(保存图像)
import numpy as np
# 导入tensorflow
import tensorflow as tf
# 导入keras
from tensorflow import keras

2.2 加载模型

把训练好的模型也加载进来,这里不用加载数据,因为数据是自制的。

# 加载my_cnn_cat_dog_3.h5文件,重新生成模型对象
recons_model = keras.models.load_model('my_cnn_cat_dog_3.h5')

2.3 设置保存图片的路径

将数据集的某个数据以图片的形式进行保存,便于测试的可视化,这里在之前已经分了测试集,因此设置图片路径即可。
在这里设置图片存储的位置,便于将图片进行存储。

# 创建图片保存路径
test_file_path = os.path.join('dog-cats', 'test', '1.jpg')
# 加载本地test.png图像
image = cv2.imread(test_file_path)

上述代码是将test文件夹里面的1.jpg进行测试,如果想测试其它的只需改为x.jpg即可。
在这里插入图片描述

2.4 加载图片

采用cv2对图片进行加载,用opencv库也就是cv2读取图片的时候,图片是三通道的,而训练的模型是三通道的,因此不只用取单通道,而是三通道,这里和之前的灰度图不同。

# 复制图片
test_img = image.copy()
# 将图片大小转换成(150,150)
test_img = cv2.resize(test_img, (150,150))

2.5 图片预处理

对图片进行预处理,即进行归一化处理和改变形状处理,这是为了便于将图片输入给训练好的模型进行预测。因此在这里将形状改变为1501503的,前面的1是样本数,所以是(1,150,150,3)。

# 预处理: 归一化 + reshape
new_test_img = (test_img/255.0).reshape(1, 150,150, 3)

2.6 对图片进行预测

将图片输入给训练好我的模型并且进行预测。
因为是二分类,所以预测的结果是1个概率值,所以需要进行处理, 大于0.5的是狗,小于0.5的是猫。

# 预测
y_pre_pro = recons_model.predict(new_test_img, verbose=1)
# 哪一类
class_id = np.argmax(y_pre_pro, axis=1)[0]
print('test.png的预测概率:', y_pre_pro)
print('test.png的预测概率:', y_pre_pro[0, class_id])
if y_pre_pro[0, class_id] > 0.5:print('png的所属类别:', 'dog')
else:print('png的所属类别:', 'cat')

2.7 显示图片

对预测的图片进行显示,把预测的数字显示在图片上。
下面5行代码分别是创建窗口,设定窗口大小,显示图片,停留图片,清除内存。

# # 显示
cv2.namedWindow('img', 0)
cv2.resizeWindow('img', 500, 500)  # 自己设定窗口图片的大小
cv2.imshow('img', image)
cv2.waitKey()
cv2.destroyAllWindows()

3. 完整代码和显示结果

以下是完整的代码和图片显示结果。

from tensorflow import keras
import skimage, os, sys, cv2
from PIL import ImageFont, Image, ImageDraw  # PIL就是pillow包(保存图像)
import numpy as np
# 导入tensorflow
import tensorflow as tf
# 导入keras
from tensorflow import keras# 加载my_cnn_cat_dog_3.h5文件,重新生成模型对象
recons_model = keras.models.load_model('my_cnn_cat_dog_3.h5')
# 创建图片保存路径
test_file_path = os.path.join('dog-cats', 'test', '1.jpg')
# 加载本地test.png图像
image = cv2.imread(test_file_path)
# 复制图片
test_img = image.copy()
# 将图片大小转换成(150,150)
test_img = cv2.resize(test_img, (150,150))
# 预处理: 归一化 + reshape
new_test_img = (test_img/255.0).reshape(1, 150,150, 3)
# 预测
y_pre_pro = recons_model.predict(new_test_img, verbose=1)
# 哪一类
class_id = np.argmax(y_pre_pro, axis=1)[0]
print('test.png的预测概率:', y_pre_pro)
print('test.png的预测概率:', y_pre_pro[0, class_id])
if y_pre_pro[0, class_id] > 0.5:print('png的所属类别:', 'dog')
else:print('png的所属类别:', 'cat')
# # 显示
cv2.namedWindow('img', 0)
cv2.resizeWindow('img', 500, 500)  # 自己设定窗口图片的大小
cv2.imshow('img', image)
cv2.waitKey()
cv2.destroyAllWindows()
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
1/1 [==============================] - 3s 3s/step
test.png的预测概率: [[0.999939]]
test.png的预测概率: 0.999939
png的所属类别: dog

在这里插入图片描述

4. 多张图片进行测试的完整代码以及结果

为了测试更多的图片,引入循环进行多次测试,效果更好。

from tensorflow import keras
from keras.datasets import cifar10
import skimage, os, sys, cv2
from PIL import ImageFont, Image, ImageDraw  # PIL就是pillow包(保存图像)
import numpy as np# 加载my_cnn_cat_dog_3.h5文件,重新生成模型对象
recons_model = keras.models.load_model('my_cnn_cat_dog_3.h5')prepicture = int(input("input the number of test picture :"))
for i in range(prepicture):path1 = input("input the test picture path:")# 创建图片保存路径test_file_path = os.path.join('dog-cats', 'test', path1)# 加载本地test.png图像image = cv2.imread(test_file_path)# 复制图片test_img = image.copy()# 将图片大小转换成(150,150)test_img = cv2.resize(test_img, (150, 150))# 预处理: 归一化 + reshapenew_test_img = (test_img / 255.0).reshape(1, 150, 150, 3)# 预测y_pre_pro = recons_model.predict(new_test_img, verbose=1)# 哪一类数字class_id = np.argmax(y_pre_pro, axis=1)[0]print('test.png的预测概率:', y_pre_pro)print('test.png的预测概率:', y_pre_pro[0, class_id])if y_pre_pro[0, class_id] > 0.5:print('png的所属类别:', 'dog')else:print('png的所属类别:', 'cat')# # 显示cv2.namedWindow('img', 0)cv2.resizeWindow('img', 500, 500)  # 自己设定窗口图片的大小cv2.imshow('img', image)cv2.waitKey()cv2.destroyAllWindows()
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
input the number of test picture :2
input the test picture path:2.jpg
1/1 [==============================] - 2s 2s/step
test.png的预测概率: [[0.99774814]]
test.png的预测概率: 0.99774814
png的所属类别: dog

在这里插入图片描述

input the test picture path:3.jpg
1/1 [==============================] - 0s 87ms/step
test.png的预测概率: [[0.9999783]]
test.png的预测概率: 0.9999783
png的所属类别: dog

在这里插入图片描述

http://www.lryc.cn/news/109178.html

相关文章:

  • React(1)——快速入门
  • 【论文】【生成对抗网络五】Wasserstein GAN (WGAN)
  • 学习率Learn_rate是什么(深度学习)
  • webpack基础知识五:说说Loader和Plugin的区别?编写Loader,Plugin的思路?
  • AI大模型之花,绽放在鸿蒙沃土
  • [JAVAee]锁策略
  • uni-app-使用tkiTree组件实现树形结构选择
  • SQL-每日一题【1179. 重新格式化部门表】
  • GO语言语法结构
  • C++学习——模板
  • 二叉树的遍历(先序遍历,中序遍历,后序遍历)递归与非递归算法
  • 【LeetCode】516. 最长回文子序列
  • Java 集合框架
  • 遇到多人协作,我们该用git如何应对?(版本二)
  • Flutter iOS 集成使用 fluter boost
  • node.js相关的npm包的集合
  • Android Ble蓝牙App(二)连接与发现服务
  • Android 自定义按钮(可滑动、点击)
  • mac录屏怎么打开?很简单,让我来教你!
  • Stable Diffusion AI绘画学习指南【插件安装设置】
  • APP开发中的性能优化:提升用户满意度的关键
  • Golang 切片 常用方法
  • 【Linux后端服务器开发】poll/epoll多路转接IO服务器
  • 【设计模式——学习笔记】23种设计模式——命令模式Command(原理讲解+应用场景介绍+案例介绍+Java代码实现)
  • Rust中的高吞吐量流处理
  • 探索编程世界的宝藏:程序员必掌握的20大算法
  • Android NFC通信示例
  • 2023年08月IDE流行度最新排名
  • 使用Beego和MySQL实现帖子和评论的应用,并进行接口测试(附源码和代码深度剖析)
  • 物联网潜在的巨大价值在于大数据分析