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

中国空间站设计在轨飞行多少年谷歌推广怎么做

中国空间站设计在轨飞行多少年,谷歌推广怎么做,3322域名注册,网络管理专业目录 1. 说明2. cifar10的CNN模型测试2.1 导入相关库2.2 加载数据和模型2.3 设置保存图片的路径2.4 加载图片2.5 图片预处理2.6 对图片进行预测2.7 显示图片 3. 完整代码和显示结果4. 多张图片进行测试的完整代码以及结果 1. 说明 本篇文章是对上篇文章训练的模型进行测试。首…

目录

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

1. 说明

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

2. cifar10的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
from keras.datasets import cifar10

2.2 加载数据和模型

把cifar10数据集进行加载,并且把训练好的模型也加载进来。

# cifar10数据集列表
class_names = ["airplane", "automobile", "bird", "cat", "deer","dog", "frog", "horse", "ship", "truck"]# 加载fashion数据
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# 加载cnn_cifar10_4.h5文件,重新生成模型对象
recons_model = keras.models.load_model('cnn_cifar10_4.h5')

2.3 设置保存图片的路径

将数据集的某个数据以图片的形式进行保存,便于测试的可视化。
在这里设置图片存储的位置。


# 创建图片保存路径
test_file_path = os.path.join(sys.path[0], 'imgs', 'test1000.png')
# 存储测试数据的任意一个
Image.fromarray(x_test[1000]).save(test_file_path)

在书写完上述代码后,需要在代码的当前路径下新建一个imgs的文件夹用于存储图片,如下。
在这里插入图片描述

执行完上述代码后就会在imgs的文件中可以发现多了一张图片,如下(下面测试了很多次)。
在这里插入图片描述

2.4 加载图片

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

# 加载本地test.png图像
image = cv2.imread(test_file_path)
# 复制图片
test_img = image.copy()
# 将图片大小转换成(32,32)
test_img = cv2.resize(test_img, (32, 32))

2.5 图片预处理

对图片进行预处理,即进行归一化处理和改变形状处理,这是为了便于将图片输入给训练好的模型进行预测。

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

2.6 对图片进行预测

将图片输入给训练好我的模型并且进行预测。
预测的结果是10个概率值,所以需要进行处理, np.argmax()是得到概率值最大值的序号,也就是预测的数字。

# 预测
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])
print('test.png的所属类别:', class_names[class_id])

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
from keras.datasets import cifar10
# cifar10数据集列表
class_names = ["airplane", "automobile", "bird", "cat", "deer","dog", "frog", "horse", "ship", "truck"]# 加载fashion数据
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# 加载cnn_cifar10_4.h5文件,重新生成模型对象
recons_model = keras.models.load_model('cnn_cifar10_4.h5')
# 创建图片保存路径
test_file_path = os.path.join(sys.path[0], 'imgs', 'test1000.png')
# 存储测试数据的任意一个
Image.fromarray(x_test[1000]).save(test_file_path)
# 加载本地test.png图像
image = cv2.imread(test_file_path)
# 复制图片
test_img = image.copy()
# 将图片大小转换成(32,32)
test_img = cv2.resize(test_img, (32, 32))
# 预处理: 归一化 + reshape
new_test_img = (test_img/255.0).reshape(1, 32, 32, 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])
print('test.png的所属类别:', class_names[class_id])
# # 显示
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 173ms/step
test.png的预测概率: [[5.1407650e-08 1.3184264e-07 1.4382408e-05 3.0730411e-03 6.6092167e-079.9690622e-01 3.4352513e-07 4.4902617e-06 5.1169474e-07 1.9515875e-07]]
test.png的预测概率: 0.9969062
test.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# cifar10数据集列表
class_names = ["airplane", "automobile", "bird", "cat", "deer","dog", "frog", "horse", "ship", "truck"]
# 加载mnist数据
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# 加载cnn_fashion.h5文件,重新生成模型对象
recons_model = keras.models.load_model('cnn_cifar10_4.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(sys.path[0], 'imgs', path1)# 存储测试数据的任意一个num = int(input("input the test picture num:"))Image.fromarray(x_test[num]).save(test_file_path)# 加载本地test.png图像image = cv2.imread(test_file_path)# 复制图片test_img = image.copy()# 将图片大小转换成(28,28)test_img = cv2.resize(test_img, (32, 32))# 预处理: 归一化 + reshapenew_test_img = (test_img/255.0).reshape(1, 32, 32, 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])print('test.png的所属类别:', class_names[class_id])# # 显示cv2.namedWindow('img', 0)cv2.resizeWindow('img', 500, 500)  # 自己设定窗口图片的大小cv2.imshow('img', image)cv2.waitKey()cv2.destroyAllWindows()
input the number of test picture :2
input the test picture path:90.jpg
input the test picture num:1
1/1 [==============================] - 0s 149ms/step
test.png的预测概率: [[1.5192369e-05 1.2153896e-03 4.3699760e-10 8.3202184e-07 6.7535249e-092.5758654e-10 2.1669943e-07 7.0233480e-12 9.9875784e-01 1.0427103e-05]]
test.png的预测概率: 0.99875784
test.png的所属类别: ship

在这里插入图片描述

input the test picture path:91.jpg
input the test picture num:3
1/1 [==============================] - 0s 144ms/step
test.png的预测概率: [[9.3968987e-01 7.0652168e-06 8.8076144e-03 3.7453551e-04 2.6135262e-029.9803242e-07 9.7372030e-08 1.5685426e-07 2.4942497e-02 4.1973537e-05]]
test.png的预测概率: 0.9396899
test.png的所属类别: airplane

在这里插入图片描述

http://www.mmbaike.com/news/59099.html

相关文章:

  • 济南商城网站建设多少钱志鸿优化设计答案
  • 网站建设费入什么科目西安网站seo哪家公司好
  • 买布做衣裳 在哪个网站买好杭州seo网站优化
  • 济宁网站制作网站关键词排名优化
  • 二次开发是指上海怎么做seo推广
  • 张家界有没有做网站的公司网页设计制作网站html代码大全
  • 怎么做搜索网站谷歌商店安卓版下载
  • 怎么识别网站是用什么语言做的百度权重什么意思
  • 日本软银集团投资了多少公司seo的形式有哪些
  • 靠谱个性化网站开发seo关键词排名怎么优化
  • 南京高端网站建设网站优化软件哪个好
  • 视觉品牌网站建设百度上免费创建网站
  • 我想学做网站上海seo培训中心
  • icp网站备案流程关键词排名优化公司哪家强
  • seo批量建站刷关键词排名系统
  • 免费做网站平台怎么优化自己网站
  • 东莞市专注网站建设公司全球最牛的搜索引擎
  • 做西式快餐店网站b站免费推广app大全
  • 太原模板建站汕头网站关键词推广
  • 吴桥做网站企业类网站有哪些例子
  • 装饰公司网页设计搜索引擎优化案例分析
  • 深圳网站建设怎样做seo是什么岗位简称
  • 网站流量10g百度快速排名系统查询
  • 沂源手机网站建设公司宁波网站建设网站排名优化
  • 苏州建网站要多少钱北京朝阳区
  • 大作设计网站官网登录入口火星时代教育培训机构官网
  • 河北做网站哪家公司好网络营销理论包括哪些
  • 网站需要兼容哪些浏览器河北seo公司
  • 如何制作产品网站模板下载百度seo策略
  • 成都网站推广哪家专业模板网站建站公司