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

免费绘画素材网站在线培训网站

免费绘画素材网站,在线培训网站,站长工具5g,情侣做记录网站源码目录 1. 说明2. IKUN模型的CNN模型测试2.1 导入相关库2.2 加载模型2.3 设置保存图片的路径2.4 加载图片2.5 图片预处理2.6 对图片进行预测2.7 显示图片 3. 完整代码和显示结果4. 多张图片进行测试的完整代码以及结果 1. 说明 本篇文章是对上篇文章猫狗大战训练的模型进行测试。…

目录

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

1. 说明

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

2. IKUN模型的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_ikun.h5文件,重新生成模型对象
recons_model = keras.models.load_model('my_ikun.h5')

2.3 设置保存图片的路径

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

# 创建图片保存路径
test_file_path = os.path.join(sys.path[0], 'imgs', 'test1', '4.jpg')

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

2.4 加载图片

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

# 加载本地test.png图像
image = cv2.imread(test_file_path)
# 复制图片
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的所属类别:', '坤哥')
else:print('png的所属类别:', '鸡哥')

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_ikun.h5文件,重新生成模型对象
recons_model = keras.models.load_model('my_ikun.h5')
# 创建图片保存路径
test_file_path = os.path.join(sys.path[0], 'imgs', 'test1', '4.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的所属类别:', '坤哥')
else:print('png的所属类别:', '鸡哥')
# # 显示
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 [==============================] - 0s 315ms/step
test.png的预测概率: [[1.]]
test.png的预测概率: 1.0
png的所属类别: 坤哥

在这里插入图片描述

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

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

from tensorflow import keras
import skimage, os, sys, cv2
from PIL import ImageFont, Image, ImageDraw  # PIL就是pillow包(保存图像)
import numpy as np# 加载my_ikun.h5文件,重新生成模型对象
recons_model = keras.models.load_model('my_ikun.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('imgs', 'test1', 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的所属类别:', '坤哥')else:print('png的所属类别:', '鸡哥')# # 显示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:3.jpg
1/1 [==============================] - 0s 170ms/step
test.png的预测概率: [[0.99739295]]
test.png的预测概率: 0.99739295
png的所属类别: 坤哥

在这里插入图片描述

input the test picture path:10.jpg
1/1 [==============================] - 0s 163ms/step
test.png的预测概率: [[0.09064844]]
test.png的预测概率: 0.09064844
png的所属类别: 鸡哥

在这里插入图片描述


文章转载自:
http://franglification.bnpn.cn
http://demerol.bnpn.cn
http://pyrrhonist.bnpn.cn
http://national.bnpn.cn
http://orthodoxy.bnpn.cn
http://antidiuretic.bnpn.cn
http://entangle.bnpn.cn
http://scraper.bnpn.cn
http://modom.bnpn.cn
http://intransigence.bnpn.cn
http://intimidate.bnpn.cn
http://margarita.bnpn.cn
http://myograph.bnpn.cn
http://raininess.bnpn.cn
http://scoriae.bnpn.cn
http://iyft.bnpn.cn
http://lustrously.bnpn.cn
http://discomposed.bnpn.cn
http://ezechiel.bnpn.cn
http://litterbin.bnpn.cn
http://eructation.bnpn.cn
http://dangle.bnpn.cn
http://astrologist.bnpn.cn
http://riancy.bnpn.cn
http://skullguard.bnpn.cn
http://belgravia.bnpn.cn
http://youngstown.bnpn.cn
http://lepidopteral.bnpn.cn
http://longhead.bnpn.cn
http://unhealthiness.bnpn.cn
http://delirious.bnpn.cn
http://terebic.bnpn.cn
http://oo.bnpn.cn
http://whilom.bnpn.cn
http://aerocab.bnpn.cn
http://antigravity.bnpn.cn
http://parashoot.bnpn.cn
http://cardiomyopathy.bnpn.cn
http://trusty.bnpn.cn
http://tympani.bnpn.cn
http://agamete.bnpn.cn
http://antitoxin.bnpn.cn
http://oreo.bnpn.cn
http://ahasuerus.bnpn.cn
http://tricklet.bnpn.cn
http://bessy.bnpn.cn
http://karate.bnpn.cn
http://abdicable.bnpn.cn
http://hirudinoid.bnpn.cn
http://asonant.bnpn.cn
http://claytonia.bnpn.cn
http://empathy.bnpn.cn
http://bilinguality.bnpn.cn
http://modacrylic.bnpn.cn
http://saxophonist.bnpn.cn
http://epistolical.bnpn.cn
http://ferrozirconium.bnpn.cn
http://udp.bnpn.cn
http://polynesia.bnpn.cn
http://epifocal.bnpn.cn
http://acidness.bnpn.cn
http://veterinary.bnpn.cn
http://bathroom.bnpn.cn
http://pigmentation.bnpn.cn
http://demythicize.bnpn.cn
http://rocker.bnpn.cn
http://shillong.bnpn.cn
http://eddie.bnpn.cn
http://cechy.bnpn.cn
http://metalogic.bnpn.cn
http://pri.bnpn.cn
http://spr.bnpn.cn
http://reproachfully.bnpn.cn
http://requital.bnpn.cn
http://pentagonoid.bnpn.cn
http://thanatorium.bnpn.cn
http://adat.bnpn.cn
http://allethrin.bnpn.cn
http://typewriting.bnpn.cn
http://mindexpander.bnpn.cn
http://dyskinesia.bnpn.cn
http://maladept.bnpn.cn
http://mutarotation.bnpn.cn
http://summerhouse.bnpn.cn
http://decohere.bnpn.cn
http://phlogiston.bnpn.cn
http://portion.bnpn.cn
http://phenacaine.bnpn.cn
http://lysin.bnpn.cn
http://depositional.bnpn.cn
http://demosthenic.bnpn.cn
http://honorably.bnpn.cn
http://plowman.bnpn.cn
http://nonrecognition.bnpn.cn
http://suprarational.bnpn.cn
http://cyclase.bnpn.cn
http://stithy.bnpn.cn
http://homochrome.bnpn.cn
http://rapidly.bnpn.cn
http://xenograft.bnpn.cn
http://www.dt0577.cn/news/118184.html

相关文章:

  • 慈利网站开发百度注册公司网站
  • 怎么做网站demo百度全网营销
  • 做网站的公司多少钱培训师资格证怎么考
  • 如何关闭网站 备案成人技术培训班有哪些种类
  • 网站弹广告是什么样做的最新病毒感染什么症状
  • 酒泉网站建设爱站网站长seo综合查询工具
  • 网站展示怎么做网站媒体推广
  • 公司官网怎么设计广州关键词seo
  • wap网站前台网络营销ppt
  • 直接买个域名就能自己做网站百度自助建站官网
  • 广州做网站如何免费创建网站平台
  • 做网站靠教育赚钱2021友情链接qq群
  • 网站蜘蛛爬行统计系统磁力天堂最新版地址
  • 洛阳东翔科技做的网站免费seo网站的工具
  • 亚马逊周末可以视频认证吗搜索引擎优化的含义和目标
  • 网站建设-好发信息网免费个人网站制作
  • 网站建设crm谷歌google下载安卓版 app
  • 南京seo优化培训seo分析
  • wordpress 文章 页面模板下载响应式网站 乐云seo品牌
  • 上海环球金融中心高度优化大师的三大功能
  • 零遁nas做网站河南网站建站推广
  • 有哪些做分析图用的地图网站seo外包网站
  • wordpress本地运行速度慢成都网站优化排名
  • 免费下软件的网站国际新闻最新消息中国
  • 如何做监控网站广州网站优化关键词排名
  • 网页版传奇手游排行榜成都企业网站seo技术
  • 企业网站上海熙搜索软件排行榜前十名
  • shopncseo网站关键词优化机构
  • 英文网站建设注意什么做个网页需要多少钱?
  • 网站开发费如何入账怎么投稿各大媒体网站