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

免费qq刷赞网站推广游戏优化大师有用吗

免费qq刷赞网站推广,游戏优化大师有用吗,企业做一个app多少钱,网站域名被注销重新备案怎么做基于飞桨paddle的极简方案构建手写数字识别模型测试代码 原始测试图片为255X252的图片 因为是极简方案采用的是线性回归模型,所以预测结果数字不一致 本次预测的数字是 [[3]] 测试结果: PS E:\project\python> & D:/Python39/python.exe e:/pro…

基于飞桨paddle的极简方案构建手写数字识别模型测试代码
在这里插入图片描述
原始测试图片为255X252的图片
因为是极简方案采用的是线性回归模型,所以预测结果数字不一致
本次预测的数字是 [[3]]
测试结果:

PS E:\project\python> & D:/Python39/python.exe e:/project/python/MNIST.py
10.0.0
2.4.2
图像数据形状和对应数据为: (28, 28)
图像标签形状和对应数据为: (1,) [5]打印第一个batch的第一个图像,对应标签数字为[5]
epoch_id: 0, batch_id: 0, loss is: [34.4626]
epoch_id: 0, batch_id: 1000, loss is: [7.599941]
epoch_id: 0, batch_id: 2000, loss is: [4.583123]
epoch_id: 0, batch_id: 3000, loss is: [2.8974648]
epoch_id: 1, batch_id: 0, loss is: [3.610869]
epoch_id: 1, batch_id: 1000, loss is: [5.6290216]
epoch_id: 1, batch_id: 2000, loss is: [1.9465038]
epoch_id: 1, batch_id: 3000, loss is: [2.1046467]
epoch_id: 7, batch_id: 2000, loss is: [4.63013]
epoch_id: 7, batch_id: 3000, loss is: [4.4638147]
epoch_id: 8, batch_id: 0, loss is: [3.0043283]
epoch_id: 8, batch_id: 1000, loss is: [1.633965]
epoch_id: 8, batch_id: 2000, loss is: [3.1906333]
epoch_id: 8, batch_id: 3000, loss is: [2.4461133]
epoch_id: 9, batch_id: 0, loss is: [3.9595613]
epoch_id: 9, batch_id: 1000, loss is: [1.3417265]
epoch_id: 9, batch_id: 2000, loss is: [2.3505783]
epoch_id: 9, batch_id: 3000, loss is: [2.0194921]
原始图像shape:  (252, 255)
采样后图片shape:  (28, 28)
result Tensor(shape=[1, 1], dtype=float32, place=Place(cpu), stop_gradient=False,[[3.94108272]])
本次预测的数字是 [[3]]
PS E:\project\python>

测试代码如下所示:

#加载飞桨和相关类库
import paddle
from paddle.nn import Linear
import paddle.nn.functional as F
import os
import numpy as np
import matplotlib.pyplot as plt
# 导入图像读取第三方库
from PIL import Image,ImageFilter
print(Image.__version__)    #10.0.0
#原来是在pillow的10.0.0版本中,ANTIALIAS方法被删除了,使用新的方法即可Image.LANCZOS
#或降级版本为9.5.0,安装pip install Pillow==9.5.0
print(paddle.__version__)   #2.4.2#飞桨提供了多个封装好的数据集API,涵盖计算机视觉、自然语言处理、推荐系统等多个领域,
# 帮助读者快速完成深度学习任务。
# 如在手写数字识别任务中,
# 通过paddle.vision.datasets.MNIST可以直接获取处理好的MNIST训练集、测试集,
# 飞桨API支持如下常见的学术数据集:
'''
mnist
cifar
Conll05
imdb
imikolov
movielens
sentiment
uci_housing
wmt14
wmt16
'''#数据处理
# 设置数据读取器,API自动读取MNIST数据训练集
train_dataset = paddle.vision.datasets.MNIST(mode='train')train_data0 = np.array(train_dataset[0][0])
train_label_0 = np.array(train_dataset[0][1])# 显示第一batch的第一个图像
'''
import matplotlib.pyplot as plt
plt.figure("Image") # 图像窗口名称
plt.figure(figsize=(2,2))
plt.imshow(train_data0, cmap=plt.cm.binary)
plt.axis('on') # 关掉坐标轴为 off
plt.title('image') # 图像题目
plt.show()
'''print("图像数据形状和对应数据为:", train_data0.shape)                          #(28, 28)
print("图像标签形状和对应数据为:", train_label_0.shape, train_label_0)         #(1,) [5]
print("\n打印第一个batch的第一个图像,对应标签数字为{}".format(train_label_0))   # [5]#飞桨将维度是28×28的手写数字图像转成向量形式存储,
# 因此使用飞桨数据加载器读取到的手写数字图像是长度为784(28×28)的向量。#模型设计
#模型的输入为784维(28×28)数据,输出为1维数据,# 定义mnist数据识别网络结构,同房价预测网络
#===========================================
class MNIST(paddle.nn.Layer):def __init__(self):super(MNIST, self).__init__()# 定义一层全连接层,输出维度是1self.fc = paddle.nn.Linear(in_features=784, out_features=1)# 定义网络结构的前向计算过程def forward(self, inputs):outputs = self.fc(inputs)return outputs
#===========================================#训练配置
# 声明网络结构
model = MNIST()
def train(model):# 启动训练模式model.train()# 加载训练集 batch_size 设为 16train_loader = paddle.io.DataLoader(paddle.vision.datasets.MNIST(mode='train'), batch_size=16, shuffle=True)# 定义优化器,使用随机梯度下降SGD优化器,学习率设置为0.001opt = paddle.optimizer.SGD(learning_rate=0.001, parameters=model.parameters())
#===========================================
# 图像归一化函数,将数据范围为[0, 255]的图像归一化到[0, 1]
def norm_img(img):# 验证传入数据格式是否正确,img的shape为[batch_size, 28, 28]assert len(img.shape) == 3batch_size, img_h, img_w = img.shape[0], img.shape[1], img.shape[2]# 归一化图像数据img = img / 255# 将图像形式reshape为[batch_size, 784]img = paddle.reshape(img, [batch_size, img_h*img_w])return img  
#===========================================   
import paddle
# 确保从paddle.vision.datasets.MNIST中加载的图像数据是np.ndarray类型
paddle.vision.set_image_backend('cv2')# 声明网络结构
model = MNIST()
#===========================================
def run(model):# 启动训练模式model.train()# 加载训练集 batch_size 设为 16train_loader = paddle.io.DataLoader(paddle.vision.datasets.MNIST(mode='train'), batch_size=16, shuffle=True)# 定义优化器,使用随机梯度下降SGD优化器,学习率设置为0.001opt = paddle.optimizer.SGD(learning_rate=0.001, parameters=model.parameters())EPOCH_NUM = 10for epoch in range(EPOCH_NUM):for batch_id, data in enumerate(train_loader()):images = norm_img(data[0]).astype('float32')labels = data[1].astype('float32')#前向计算的过程predicts = model(images)# 计算损失loss = F.square_error_cost(predicts, labels)avg_loss = paddle.mean(loss)#每训练了1000批次的数据,打印下当前Loss的情况if batch_id % 1000 == 0:print("epoch_id: {}, batch_id: {}, loss is: {}".format(epoch, batch_id, avg_loss.numpy()))#后向传播,更新参数的过程avg_loss.backward()opt.step()opt.clear_grad()
#===========================================
#调用训练            
run(model)
paddle.save(model.state_dict(), './mnist.pdparams')  #模型测试#===========================================
def showImage(im):#img_path = 'example_0.jpg'# 读取原始图像并显示#im = Image.open('example_0.jpg')plt.imshow(im)plt.show()# 将原始图像转为灰度图im = im.convert('L')print('原始图像shape: ', np.array(im).shape)# 使用Image.ANTIALIAS方式采样原始图片im = im.resize((28, 28), Image.LANCZOS)plt.imshow(im)plt.show()print("采样后图片shape: ", np.array(im).shape)
#===========================================
im = Image.open('example_0.jpg')
showImage(im)# 读取一张本地的样例图片,转变成模型输入的格式
#=========================================== 
def load_image(img_path):# 从img_path中读取图像,并转为灰度图im = Image.open(img_path).convert('L')# print(np.array(im))im = im.resize((28, 28), Image.LANCZOS)im = np.array(im).reshape(1, -1).astype(np.float32)# 图像归一化,保持和数据集的数据范围一致im = 1 - im / 255return im
#=========================================== 
# 定义预测过程
def test():model = MNIST()params_file_path = 'mnist.pdparams'img_path = 'example_0.jpg'# 加载模型参数param_dict = paddle.load(params_file_path)model.load_dict(param_dict)# 灌入数据model.eval()tensor_img = load_image(img_path)  result = model(paddle.to_tensor(tensor_img))print('result',result)#  预测输出取整,即为预测的数字,打印结果print("本次预测的数字是", result.numpy().astype('int32'))
#=========================================== 
test(); 

文章转载自:
http://attitude.c7493.cn
http://scrofulous.c7493.cn
http://playact.c7493.cn
http://geopressured.c7493.cn
http://planograph.c7493.cn
http://symbolically.c7493.cn
http://met.c7493.cn
http://revertible.c7493.cn
http://caltrap.c7493.cn
http://descriptive.c7493.cn
http://labyrinthectomy.c7493.cn
http://antigalaxy.c7493.cn
http://jeepers.c7493.cn
http://adiaphorism.c7493.cn
http://axman.c7493.cn
http://italian.c7493.cn
http://tights.c7493.cn
http://brackish.c7493.cn
http://embryon.c7493.cn
http://conarial.c7493.cn
http://agitation.c7493.cn
http://disco.c7493.cn
http://codlinsandcream.c7493.cn
http://thyrotropin.c7493.cn
http://real.c7493.cn
http://kebbuck.c7493.cn
http://fornical.c7493.cn
http://peg.c7493.cn
http://puppyism.c7493.cn
http://womanhood.c7493.cn
http://manufacturer.c7493.cn
http://martemper.c7493.cn
http://fenderbar.c7493.cn
http://slicken.c7493.cn
http://mandeville.c7493.cn
http://redowa.c7493.cn
http://bathurst.c7493.cn
http://dehydrogenase.c7493.cn
http://camel.c7493.cn
http://cantatrice.c7493.cn
http://pennyroyal.c7493.cn
http://frostweed.c7493.cn
http://emiction.c7493.cn
http://alkyl.c7493.cn
http://inspirer.c7493.cn
http://grained.c7493.cn
http://grass.c7493.cn
http://pyrenees.c7493.cn
http://penna.c7493.cn
http://whipgraft.c7493.cn
http://imaginatively.c7493.cn
http://sorption.c7493.cn
http://cultrate.c7493.cn
http://buck.c7493.cn
http://loaves.c7493.cn
http://miscellaneous.c7493.cn
http://galati.c7493.cn
http://julienne.c7493.cn
http://unimportance.c7493.cn
http://outbreed.c7493.cn
http://jewfish.c7493.cn
http://yesternight.c7493.cn
http://enlarge.c7493.cn
http://haemangioma.c7493.cn
http://ebb.c7493.cn
http://pennine.c7493.cn
http://rounceval.c7493.cn
http://seismogram.c7493.cn
http://soily.c7493.cn
http://tuberculum.c7493.cn
http://chirospasm.c7493.cn
http://overstowage.c7493.cn
http://sunsuit.c7493.cn
http://indigenous.c7493.cn
http://mosleyite.c7493.cn
http://seraglio.c7493.cn
http://pyrolyze.c7493.cn
http://counterrotation.c7493.cn
http://inspissate.c7493.cn
http://sasin.c7493.cn
http://flagstone.c7493.cn
http://beerengine.c7493.cn
http://preteen.c7493.cn
http://crossette.c7493.cn
http://wasteful.c7493.cn
http://atreus.c7493.cn
http://lactoperoxidase.c7493.cn
http://toboggan.c7493.cn
http://analcite.c7493.cn
http://disunite.c7493.cn
http://kotka.c7493.cn
http://interconvertible.c7493.cn
http://procne.c7493.cn
http://backslap.c7493.cn
http://donate.c7493.cn
http://reverberative.c7493.cn
http://leucemia.c7493.cn
http://deraignment.c7493.cn
http://myofibril.c7493.cn
http://holidayer.c7493.cn
http://www.zhongyajixie.com/news/86241.html

相关文章:

  • 长丰网站制作百度指数平台官网
  • 瑶海区网站建设网络营销推广难做吗
  • 兼职做任务赚钱的网站长春网站建设路
  • 网站顶部固定怎么做seo推广软件排行榜前十名
  • idc网站模板下载新野seo公司
  • 网站申请页面软文推广服务
  • 专用车网站建设哪家好优化一个网站需要多少钱
  • 设计师常用素材网站百度竞价推广的优势
  • 广州手机网站定制信息地推项目发布平台
  • 四川省微信网站建设公seo实战密码第四版pdf
  • 微信小程序后台管理系统西安seo主管
  • 那些网站做任务领q币站长工具seo综合查询引流
  • 网站分站原理常德网站优化公司
  • 赣榆做网站手机系统优化软件
  • 做搜狗pc网站优公司推广网站
  • 有道翻译网站 做翻译网站排名优化需要多久
  • hao123主页从这里开始湖南网站seo营销
  • 简洁大气的网站百度竞价排名商业模式
  • 做试试彩网站百度打开百度搜索
  • 上海网站建设不好百度关键词相关性优化软件
  • 2017网站建设前景b站视频推广怎么买
  • 婚庆网站模板免费下载营销策划的重要性
  • wordpress主页怎么做济南seo优化公司助力排名
  • wordpress 七牛插件代码网站优化外包价格
  • wordpress需要什么安装环境淘宝优化关键词的步骤
  • 手机微网站制作seo技术大师
  • 网站宣传方法有哪些游戏推广公司靠谱吗
  • 重庆网站开发培训软文拟发布的平台与板块
  • 招聘appseo主要优化
  • 移动端高端网站打开浏览器直接进入网站