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

长沙企业网站开发哪家专业福田祥菱m2怎么样

长沙企业网站开发哪家专业,福田祥菱m2怎么样,郑州网站建设公司排行,wordpress批量目录 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.yayakq.cn/news/595738/

相关文章:

  • 制作网站需要哪些知识大连seo快速排名
  • 河北省住房城乡建设厅网站apache 多个网站
  • 西安浐灞生态区规划建设局网站前端培训出来工资多少
  • 网站开发简称微营销
  • 福州哪家网站制作设计高端还实惠怎样用网站模板做网站
  • 网站建设以什么盈利中国新闻社领导名单
  • 没有网站可以icp备案化妆品电子商务网站开发流程描述
  • 南昌集团制作网站设计wordpress 点点主题
  • 网站命名的原则包括有没有专门招代理的网站
  • 做造价在哪个网站查价格技术培训ui设计
  • 宁波网站建设zj95wordpress sql 查询
  • 黑群晖架设wordpress中国移动网络优化做什么的
  • 建设网站项目概述山东省招投标信息网
  • 网站制作 文案dw做网站背景图片设置
  • 网站开发流程及顺序抖音优化是什么意思
  • 网站开发名片域名估价哪个网站准确
  • 做网站的分析报告案例网页设计尺寸早起可视尺寸
  • 17做网店这个网站做起多少钱陈金凌 wordpress
  • 网站建设收费标准服务网站开发项目经验描述
  • js模板网站西乡做网站费用
  • 中国建设人才网信息网站网站建设优化服务平台
  • 海南省海口市建设厅网站怎么做一键添加信任网站
  • wordpress设置置顶文章整站优化深圳
  • 成都建站提供商网站建设的安全应该注意什么
  • 可信网站验证 费用一般做建设的是什么公司
  • 智能科普网站平台建设方案开发一个网站需要多长时间
  • 西华县建设局网站app是基于什么开发的
  • 长乐建设局网站wordpress 标题相同
  • access 数据库做网站中国建设银行网站查询
  • 汽车建设网站开发流程佳能网站建设需求报告