简 介: ※通过测试网络上的这个极简的Paddle识别MNIST程序,也就是使用了一个非常简单的线性回归网络,初步熟悉了Paddle下的网络架构方式。对于如果从numpy到Paddle的tensor转换程序中也给出了示例。
关键词
: AI Studio,Paddle,MNIST
§01 建立工程
在AI Studio中建立基于NoteBook工程环境,选择其中的MNIST数据库。
在 在哪里能找到最后的版本的示例程序? AI Studio-MNIST 对于基于AI Studio中Paddle框架对于MNIST数据库进行实验。首先试验了其中的极简测试方法。但在其中过程中还是遇到了一些问题。
后来经过询问,可以知道现在书上的代码由于书籍出版比较慢,因此跟班上AI Studio代码的升级。建议还是通过观察 AI Studio 手写数字识别案例 ,根据其中的的代码进行测试。
▲ 图1.1 百度AI Studio 手写数字识别案例(上):讲师:淘淘
一、调入数据库
import matplotlib.pyplot as plt
from numpy import *
import math,time
import paddle
from paddle.nn import Linear
import paddle.nn.functional as F
import os
train_dataset = paddle.vision.datasets.MNIST(mode='train')
import paddle
from paddle.nn import Linear
import paddle.nn.functional as F
import os
train_dataset = paddle.vision.datasets.MNIST(mode='train')
train_data0 = array(train_dataset[0][0])
train_label0 = array(train_dataset[0][1])
plt.figure('Image')
plt.figure(figsize=(5,5))
plt.imshow(train_data0, cmap=plt.cm.binary)
plt.axis('on')
plt.title('MNIST image')
plt.show()
print('Image shape: {}'.format(train_data0.shape))
print('Image label shape: {} and data: {}'.format(train_label0.shape, train_label0))
▲ 图1.1.1 显示数据库图片
▲ 图1.1.2 显示MNIST中的数字
2、常见学术数据集合
在paddle.vision.datasets存在一些常见到的学术数据集合。
(1)paddle.vision 中的数据集合
dir(paddle.vision.datasets)
['Cifar10',
'Cifar100',
'DatasetFolder',
'FashionMNIST',
'Flowers',
'ImageFolder',
'MNIST',
'VOC2012',
'__all__',
'__builtins__',
'__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__path__',
'__spec__',
'cifar',
'flowers',
'folder',
'mnist',
'voc2012']
(2)paddle.text 数据集合
dir(paddle.text.datasets)
['Conll05st',
'Imdb',
'Imikolov',
'Movielens',
'UCIHousing',
'WMT14',
'WMT16',
'__all__',
'__builtins__',
'__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__path__',
'__spec__',
'conll05',
'imdb',
'imikolov',
'movielens',
'uci_housing',
'wmt14',
'wmt16']
二、极简工程
1、建立模型
class MNIST(paddle.nn.Layer):
def __init__(self, ):
super(MNIST, self).__init__()
self.fc = paddle.nn.Linear(in_features=784, out_features=1)
def forward(self, inputs):
outputs = self.fc(inputs)
return outputs
def norm_img(img):
assert len(img.shape) == 3
batch_size, img_h, img_w = img.shape[0], img.shape[1], img.shape[2]
img = img/255
img = paddle.reshape(img, [batch_size, img_h*img_w])
return img
import paddle
paddle.vision.set_image_backend('cv2')
def train(model):
model.train()
train_loader = paddle.io.DataLoader(paddle.vision.datasets.MNIST(mode='train'),
batch_size=16,
shuffle=True)
opt = paddle.optimizer.SGD(learning_rate=0.001, parameters=model.parameters())
EPOCH_NUM = 10
for 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)
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()
2、训练模型
model = MNIST()
train(model)
paddle.save(model.state_dict(), './mnist.pdparms')
3、训练结果
epoch_id: 0, batch_id: 0, loss is: [19.446383]
epoch_id: 0, batch_id: 1000, loss is: [4.280066]
epoch_id: 0, batch_id: 2000, loss is: [4.089441]
epoch_id: 0, batch_id: 3000, loss is: [2.5934415]
epoch_id: 1, batch_id: 0, loss is: [5.005641]
epoch_id: 1, batch_id: 1000, loss is: [2.2887247]
epoch_id: 1, batch_id: 2000, loss is: [2.5260096]
epoch_id: 1, batch_id: 3000, loss is: [4.377707]
epoch_id: 2, batch_id: 0, loss is: [3.2349763]
epoch_id: 2, batch_id: 1000, loss is: [2.8085265]
epoch_id: 2, batch_id: 2000, loss is: [2.2175798]
epoch_id: 2, batch_id: 3000, loss is: [5.4343185]
epoch_id: 3, batch_id: 0, loss is: [3.1255033]
epoch_id: 3, batch_id: 1000, loss is: [2.1449356]
epoch_id: 3, batch_id: 2000, loss is: [7.3950243]
epoch_id: 3, batch_id: 3000, loss is: [5.631453]
epoch_id: 4, batch_id: 0, loss is: [2.1221619]
epoch_id: 4, batch_id: 1000, loss is: [3.1189494]
epoch_id: 4, batch_id: 2000, loss is: [3.672319]
epoch_id: 4, batch_id: 3000, loss is: [4.128253]
epoch_id: 5, batch_id: 0, loss is: [7.7472067]
epoch_id: 5, batch_id: 1000, loss is: [2.6192496]
epoch_id: 5, batch_id: 2000, loss is: [3.7988458]
epoch_id: 5, batch_id: 3000, loss is: [2.1571586]
epoch_id: 6, batch_id: 0, loss is: [6.8091993]
epoch_id: 6, batch_id: 1000, loss is: [3.2879863]
epoch_id: 6, batch_id: 2000, loss is: [2.2202625]
epoch_id: 6, batch_id: 3000, loss is: [4.0542073]
epoch_id: 7, batch_id: 0, loss is: [2.4702597]
epoch_id: 7, batch_id: 1000, loss is: [3.267303]
epoch_id: 7, batch_id: 2000, loss is: [3.925469]
epoch_id: 7, batch_id: 3000, loss is: [4.502317]
epoch_id: 8, batch_id: 0, loss is: [1.6059736]
epoch_id: 8, batch_id: 1000, loss is: [5.4941883]
epoch_id: 8, batch_id: 2000, loss is: [1.0239292]
epoch_id: 8, batch_id: 3000, loss is: [2.333592]
epoch_id: 9, batch_id: 0, loss is: [2.7579784]
epoch_id: 9, batch_id: 1000, loss is: [1.5081773]
epoch_id: 9, batch_id: 2000, loss is: [4.925281]
epoch_id: 9, batch_id: 3000, loss is: [3.8142138]
▲ 图1.2.1 训练过程中误差下降曲线
4、测试模型
(1)查看测试集合结果
for batch_id, data in enumerate(test_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)
print(predicts)
print(labels)
print(loss)
print(avg_loss)
break
运行结果
Tensor(shape=[16, 1], dtype=float32, place=CPUPlace, stop_gradient=False,
[[2.06245565],
[1.97789598],
[5.32851791],
[2.76517129],
[4.77754116],
[1.96410847],
[1.70493352],
[2.46705198],
[7.93237495],
[5.77034092],
[4.87852144],
[0.48723245],
[4.39118719],
[1.38979697],
[1.77543545],
[1.47215056]])
Tensor(shape=[16, 1], dtype=float32, place=CPUPlace, stop_gradient=True,
[[0.],
[1.],
[5.],
[5.],
[6.],
[1.],
[1.],
[1.],
[7.],
[4.],
[2.],
[0.],
[1.],
[1.],
[0.],
[1.]])
Tensor(shape=[16, 1], dtype=float32, place=CPUPlace, stop_gradient=False,
[[4.25372314],
[0.95628053],
[0.10792402],
[4.99445915],
[1.49440563],
[0.92950511],
[0.49693128],
[2.15224147],
[0.86932307],
[3.13410687],
[8.28588581],
[0.23739545],
[11.50015068],
[0.15194169],
[3.15217113],
[0.22292615]])
Tensor(shape=[1], dtype=float32, place=CPUPlace, stop_gradient=False,
[2.68371058])
(2)测试预测结果
def showimg(img):
imgdata = img.numpy()
print(imgdata.shape)
imgblock = [i.reshape([28,28]) for i in imgdata]
imgb1 = concatenate(imgblock[:8], axis=1)
imgb2 = concatenate(imgblock[8:], axis=1)
imgb = concatenate((imgb1, imgb2))
plt.figure(figsize=(10,10))
plt.imshow(imgb)
plt.axis('off')
plt.show()
▲ 图1.2.2 测试集合MNIST图片
预测结果:
for batch_id, data in enumerate(test_loader()):
showimg(images)
predicts = model(images)
print(labels.numpy().flatten().T)
print([p for p in predicts.numpy().flatten().T])
3. | 9. | 4. | 3. | 0. | 7. | 0. | 9. | 9. | 5. | 6. | 7. | 1. | 7. | 0. | 0. |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
3.3602648 | 8.111923 | 5.3560495 | 5.2887278 | 4.218868 | 5.3987856 | 2.5647051 | 8.387244 | 8.198596 | 3.977576 | 3.7429187 | 7.7407055 | 6.2851562 | 4.435977 | 2.9352028 | 3.7802896 |
5、网络上参考程序
import paddle
from paddle.nn import Linear
import paddle.nn.functional as F
import os
import numpy as np
import matplotlib.pyplot as plt
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])
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)
print("图像标签形状和对应数据为:", train_label_0.shape, train_label_0)
print("\n打印第一个batch的第一个图像,对应标签数字为{}".format(train_label_0))
class MNIST(paddle.nn.Layer):
def __init__(self):
super(MNIST, self).__init__()
# 定义一层全连接层,输出维度是1
self.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 设为 16
train_loader = paddle.io.DataLoader(paddle.vision.datasets.MNIST(mode='train'),
batch_size=16,
shuffle=True)
# 定义优化器,使用随机梯度下降SGD优化器,学习率设置为0.001
opt = paddle.optimizer.SGD(learning_rate=0.001, parameters=model.parameters())
def norm_img(img):
# 验证传入数据格式是否正确,img的shape为[batch_size, 28, 28]
assert len(img.shape) == 3
batch_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.set_image_backend('cv2')
model = MNIST()
def train(model):
# 启动训练模式
model.train()
# 加载训练集 batch_size 设为 16
train_loader = paddle.io.DataLoader(paddle.vision.datasets.MNIST(mode='train'),
batch_size=16,
shuffle=True)
# 定义优化器,使用随机梯度下降SGD优化器,学习率设置为0.001
opt = paddle.optimizer.SGD(learning_rate=0.001, parameters=model.parameters())
EPOCH_NUM = 10
for 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()
train(model)
paddle.save(model.state_dict(), './mnist.pdparams')
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
img_path = './work/下载.png'
im = Image.open('./work/下载.png')
plt.imshow(im)
plt.show()
im = im.convert('L')
print('原始图像shape: ', np.array(im).shape)
im = im.resize((28, 28), Image.ANTIALIAS)
plt.imshow(im)
plt.show()
print("采样后图片shape: ", np.array(im).shape)
def load_image(img_path):
# 从img_path中读取图像,并转为灰度图
im = Image.open(img_path).convert('L')
# print(np.array(im))
im = im.resize((28, 28), Image.ANTIALIAS)
im = np.array(im).reshape(1, -1).astype(np.float32)
# 图像归一化,保持和数据集的数据范围一致
im = 1 - im / 255
return im
model = MNIST()
params_file_path = 'mnist.pdparams'
img_path = './work/下载.png'
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'))
※ 识别总结 ※
通过测试网络上的这个极简的Paddle识别MNIST程序,也就是使用了一个非常简单的线性回归网络,初步熟悉了Paddle下的网络架构方式。对于如果从numpy到Paddle的tensor转换程序中也给出了示例。
一、数据转换
1、从numpy到tensor
paddle.to_tensor()
2、从tensor到numpy
data.numpy()
■ 相关文献链接:
● 相关图表链接:
- 图1.1 百度AI Studio 手写数字识别案例(上):讲师:淘淘
- 图1.1.1 显示数据库图片
- 图1.1.2 显示MNIST中的数字
- 图1.2.1 训练过程中误差下降曲线
- 图1.2.2 测试集合MNIST图片