import paddle
from paddle.nn import Linear
import paddle.nn.functional as F
import os
import numpy as np
import matplotlib.pyplot as plt
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/__init__.py:107: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
from collections import MutableMapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
from collections import Iterable, Mapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
from collections import Sized
2021-10-19 18:34:17,341 - INFO - font search path ['/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf', '/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/afm', '/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/pdfcorefonts']
2021-10-19 18:34:17,792 - INFO - generated new fontManager
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)
print("图像标签形状和对应数据为:", train_label_0.shape, train_label_0)
print("\n打印第一个batch的第一个图像,对应标签数字为{}".format(train_label_0))
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2349: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
if isinstance(obj, collections.Iterator):
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2366: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
return list(data) if isinstance(data, collections.MappingView) else data
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/numpy/lib/type_check.py:546: DeprecationWarning: np.asscalar(a) is deprecated since NumPy v1.16, use a.item() instead
'a.item() instead', DeprecationWarning, stacklevel=1)
<Figure size 432x288 with 0 Axes>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hfcsSrhI-1634642168477)(output_2_2.png)]
图像数据形状和对应数据为: (28, 28)
图像标签形状和对应数据为: (1,) [5]
打印第一个batch的第一个图像,对应标签数字为[5]
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.datasets.MNIST中加载的图像数据是np.ndarray类型
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')
epoch_id: 0, batch_id: 0, loss is: [18.786789]
epoch_id: 0, batch_id: 1000, loss is: [6.415824]
epoch_id: 0, batch_id: 2000, loss is: [2.3258514]
epoch_id: 0, batch_id: 3000, loss is: [3.4202943]
epoch_id: 1, batch_id: 0, loss is: [5.929326]
epoch_id: 1, batch_id: 1000, loss is: [3.151496]
epoch_id: 1, batch_id: 2000, loss is: [2.102501]
epoch_id: 1, batch_id: 3000, loss is: [4.243863]
epoch_id: 2, batch_id: 0, loss is: [5.80577]
epoch_id: 2, batch_id: 1000, loss is: [2.7574587]
epoch_id: 2, batch_id: 2000, loss is: [3.5232291]
epoch_id: 2, batch_id: 3000, loss is: [3.6954677]
epoch_id: 3, batch_id: 0, loss is: [2.2268412]
epoch_id: 3, batch_id: 1000, loss is: [6.5388727]
epoch_id: 3, batch_id: 2000, loss is: [1.9364144]
epoch_id: 3, batch_id: 3000, loss is: [6.139535]
epoch_id: 4, batch_id: 0, loss is: [3.810705]
epoch_id: 4, batch_id: 1000, loss is: [3.8114722]
epoch_id: 4, batch_id: 2000, loss is: [2.5466309]
epoch_id: 4, batch_id: 3000, loss is: [1.0090096]
epoch_id: 5, batch_id: 0, loss is: [3.683055]
epoch_id: 5, batch_id: 1000, loss is: [1.5672]
epoch_id: 5, batch_id: 2000, loss is: [5.5539384]
epoch_id: 5, batch_id: 3000, loss is: [3.6176972]
epoch_id: 6, batch_id: 0, loss is: [4.3535347]
epoch_id: 6, batch_id: 1000, loss is: [4.564929]
epoch_id: 6, batch_id: 2000, loss is: [3.0655735]
epoch_id: 6, batch_id: 3000, loss is: [8.249016]
epoch_id: 7, batch_id: 0, loss is: [3.7796612]
epoch_id: 7, batch_id: 1000, loss is: [2.493836]
epoch_id: 7, batch_id: 2000, loss is: [3.1261213]
epoch_id: 7, batch_id: 3000, loss is: [2.2971213]
epoch_id: 8, batch_id: 0, loss is: [3.3106918]
epoch_id: 8, batch_id: 1000, loss is: [2.578835]
epoch_id: 8, batch_id: 2000, loss is: [1.344795]
epoch_id: 8, batch_id: 3000, loss is: [1.7976007]
epoch_id: 9, batch_id: 0, loss is: [2.4240432]
epoch_id: 9, batch_id: 1000, loss is: [1.9495399]
epoch_id: 9, batch_id: 2000, loss is: [2.5719266]
epoch_id: 9, batch_id: 3000, loss is: [3.4642498]
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
img_path = './work/example_0.jpg'
# 读取原始图像并显示
im = Image.open('./work/example_0.png')
plt.imshow(im)
plt.show()
# 将原始图像转为灰度图
im = im.convert('L')
print('原始图像shape: ', np.array(im).shape)
# 使用Image.ANTIALIAS方式采样原始图片
im = im.resize((28, 28), Image.ANTIALIAS)
plt.imshow(im)
plt.show()
print("采样后图片shape: ", np.array(im).shape)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-by8wWQyw-1634642168479)(output_7_0.png)]
原始图像shape: (892, 1195)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4iVpP4QX-1634642168480)(output_7_2.png)]
采样后图片shape: (28, 28)
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/example_0.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'))
result Tensor(shape=[1, 1], dtype=float32, place=CPUPlace, stop_gradient=False,
[[4.13501024]])
本次预测的数字是 [[4]]
import paddle
from paddle.nn import Linear
import paddle.nn.functional as F
import os
import gzip
import json
import random
import numpy as np
datafile = './work/mnist.json.gz'
print('loading mnist dataset from {} ......'.format(datafile))
# 加载json数据文件
data = json.load(gzip.open(datafile))
print('mnist dataset load done')
# 读取到的数据区分训练集,验证集,测试集
train_set, val_set, eval_set = data
# 观察训练集数据
imgs, labels = train_set[0], train_set[1]
print("训练数据集数量: ", len(imgs))
# 观察验证集数量
imgs, labels = val_set[0], val_set[1]
print("验证数据集数量: ", len(imgs))
# 观察测试集数量
imgs, labels = val= eval_set[0], eval_set[1]
n(imgs))
# 观察验证集数量
imgs, labels = val_set[0], val_set[1]
print("验证数据集数量: ", len(imgs))
# 观察测试集数量
imgs, labels = val= eval_set[0], eval_set[1]
print("测试数据集数量: ", len(imgs))
loading mnist dataset from ./work/mnist.json.gz ......
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-18-88d2b5cc34b5> in <module>
2 print('loading mnist dataset from {} ......'.format(datafile))
3 # 加载json数据文件
----> 4 data = json.load(gzip.open(datafile))
5 print('mnist dataset load done')
6 # 读取到的数据区分训练集,验证集,测试集
/opt/conda/envs/python35-paddle120-env/lib/python3.7/gzip.py in open(filename, mode, compresslevel, encoding, errors, newline)
51 gz_mode = mode.replace("t", "")
52 if isinstance(filename, (str, bytes, os.PathLike)):
---> 53 binary_file = GzipFile(filename, gz_mode, compresslevel)
54 elif hasattr(filename, "read") or hasattr(filename, "write"):
55 binary_file = GzipFile(None, gz_mode, compresslevel, filename)
/opt/conda/envs/python35-paddle120-env/lib/python3.7/gzip.py in __init__(self, filename, mode, compresslevel, fileobj, mtime)
161 mode += 'b'
162 if fileobj is None:
--> 163 fileobj = self.myfileobj = builtins.open(filename, mode or 'rb')
164 if filename is None:
165 filename = getattr(fileobj, 'name', '')
FileNotFoundError: [Errno 2] No such file or directory: './work/mnist.json.gz'