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

上海建设部网站首页网站优化服务流程

上海建设部网站首页,网站优化服务流程,dw做网站教程,自己做的网站维护一年多少钱目录 1. 说明2. 猫狗大战的CNN模型测试2.1 导入相关库2.2 加载模型2.3 设置保存图片的路径2.4 加载图片2.5 图片预处理2.6 对图片进行预测2.7 显示图片 3. 完整代码和显示结果4. 多张图片进行测试的完整代码以及结果 1. 说明 本篇文章是对上篇文章猫狗大战训练的模型进行测试。…

目录

  • 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.15wanjia.com/news/154991.html

相关文章:

  • 遵义营商环境建设局网站美食网站开发方案
  • 网站标签设计竹子建站免费版
  • 企业网站开发价格怎么做网站的关键词
  • 有那个网站可以做免费的投票旅游网站前台模板
  • 做网页收集素材常用的网站有哪些酒店网站建设哪家好
  • 做企业网站需要哪些网络营销思想的网站改版计划
  • wordpress网站下载做ppt图片用的网站有哪些
  • 网站建设与维护考试卷国家高新技术企业证书图片
  • 简单的网站更新 关键词优化 关键词互联百姓网上海招聘
  • 网站设计 网站建设 手机网站建设电子商务专业学什么
  • 模板网站平台漳州seo建站
  • 图片库网站建设wordpress 微信分享h5
  • dede网站打开速度慢陕西省建设安全协会网站
  • 公司核名在哪个网站柘林网站建设
  • 乔拓云建站有免费的吗信阳网站建设找汉狮
  • 更好的网站制作广告公司岗位
  • 武夷山市建设局网站国外网站源码
  • 网站建设分几种类型陕西大型网站建设
  • 深圳龙岗企业网站建设简洁中文网站模板下载
  • 网站建设管理实训报告网站建设与管理就业
  • 黔南州建设局门户网站手机网站域名开头
  • 敦煌网站外引流怎么做广州抖音推广
  • 安徽先锋网站两学一做买一个网页多少钱
  • 谁有做网站比较厉害的提升网站建设品质
  • 科目一速成网站建设个人博客ui设计
  • 郴州网站制作公司在哪里做电商看的网站有哪些内容
  • 有什么网站可以做投票功能吗中国500强企业排名完整版
  • 国外网站源代码广州市开发区建设网站
  • 网站蜘蛛记录怎么给网站添加统计代码
  • 网站名字备案流程厦门网站制作费用明细