# 查看当前挂载的数据集目录, 该目录下的变更重启环境后会自动还原
# View dataset directory.
# This directory will be recovered automatically after resetting environment.
!ls /home/aistudio/data
# 查看工作区文件, 该目录下的变更将会持久保存. 请及时清理不必要的文件, 避免加载过慢.
# View personal work directory.
# All changes under this directory will be kept even after reset.
# Please clean unnecessary files in time to speed up environment loading.
!ls /home/aistudio/work
# 如果需要进行持久化安装, 需要使用持久化路径, 如下方代码示例:
# If a persistence installation is required,
# you need to use the persistence path as the following:
!mkdir /home/aistudio/external-libraries
!pip install beautifulsoup4 -t /home/aistudio/external-libraries
# 同时添加如下代码, 这样每次环境(kernel)启动的时候只要运行下方代码即可:
# Also add the following code,
# so that every time the environment (kernel) starts,
# just run the following code:
import sys
sys.path.append('/home/aistudio/external-libraries')
请点击此处查看本环境基本用法.
Please click here for more detailed instructions.
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')
Cache file /home/aistudio/.cache/paddle/dataset/mnist/train-images-idx3-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/train-images-idx3-ubyte.gz
Begin to download
Download finished
Cache file /home/aistudio/.cache/paddle/dataset/mnist/train-labels-idx1-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/train-labels-idx1-ubyte.gz
Begin to download
........
Download finished
train_data0 = np.array(train_dataset[1][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')
plt.title('image')
plt.show()
print("图像数据形状和对应数据为:", train_data0.shape)
print("图像标签形状和对应数据为:", train_label_0.shape, train_label_0)
print("\n打印第一个batch的第一个图像,对应标签数字为{}".format(train_label_0))
<Figure size 432x288 with 0 Axes>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TtNDdVF7-1634644004682)(output_7_1.png)]
图像数据形状和对应数据为: (28, 28)
图像标签形状和对应数据为: (1,) [5]
打印第一个batch的第一个图像,对应标签数字为[5]
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
model = MNIST()
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())
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')
model = MNIST()
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()
train(model)
paddle.save(model.state_dict(), './mnist.pdparams')
epoch_id: 0, batch_id: 0, loss is: [27.761705]
epoch_id: 0, batch_id: 1000, loss is: [3.3254228]
epoch_id: 0, batch_id: 2000, loss is: [3.8109272]
epoch_id: 0, batch_id: 3000, loss is: [3.5200453]
epoch_id: 1, batch_id: 0, loss is: [4.3337345]
epoch_id: 1, batch_id: 1000, loss is: [4.4794836]
epoch_id: 1, batch_id: 2000, loss is: [2.6495423]
epoch_id: 1, batch_id: 3000, loss is: [3.0071836]
epoch_id: 2, batch_id: 0, loss is: [5.1993294]
epoch_id: 2, batch_id: 1000, loss is: [2.851439]
epoch_id: 2, batch_id: 2000, loss is: [3.3957996]
epoch_id: 2, batch_id: 3000, loss is: [3.0392585]
epoch_id: 3, batch_id: 0, loss is: [4.074491]
epoch_id: 3, batch_id: 1000, loss is: [4.358707]
epoch_id: 3, batch_id: 2000, loss is: [3.8639283]
epoch_id: 3, batch_id: 3000, loss is: [4.586548]
epoch_id: 4, batch_id: 0, loss is: [3.8984923]
epoch_id: 4, batch_id: 1000, loss is: [4.1077127]
epoch_id: 4, batch_id: 2000, loss is: [3.5833657]
epoch_id: 4, batch_id: 3000, loss is: [1.2399864]
epoch_id: 5, batch_id: 0, loss is: [2.008752]
epoch_id: 5, batch_id: 1000, loss is: [1.9820006]
epoch_id: 5, batch_id: 2000, loss is: [2.6696982]
epoch_id: 5, batch_id: 3000, loss is: [1.7487084]
epoch_id: 6, batch_id: 0, loss is: [1.153321]
epoch_id: 6, batch_id: 1000, loss is: [3.472978]
epoch_id: 6, batch_id: 2000, loss is: [5.595134]
epoch_id: 6, batch_id: 3000, loss is: [4.1964865]
epoch_id: 7, batch_id: 0, loss is: [4.013738]
epoch_id: 7, batch_id: 1000, loss is: [5.8451033]
epoch_id: 7, batch_id: 2000, loss is: [3.546811]
epoch_id: 7, batch_id: 3000, loss is: [4.321777]
epoch_id: 8, batch_id: 0, loss is: [2.3754823]
epoch_id: 8, batch_id: 1000, loss is: [3.4823356]
epoch_id: 8, batch_id: 2000, loss is: [2.7280698]
epoch_id: 8, batch_id: 3000, loss is: [2.4496922]
epoch_id: 9, batch_id: 0, loss is: [3.3046498]
epoch_id: 9, batch_id: 1000, loss is: [5.7598853]
epoch_id: 9, batch_id: 2000, loss is: [4.2866035]
epoch_id: 9, batch_id: 3000, loss is: [2.7029982]
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
img_path = './work/example_3.jpg'
im = Image.open('./work/example_3.jpg')
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)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oIFYyVbD-1634644004684)(output_12_0.png)]
原始图像shape: (28, 28)
采样后图片shape: (28, 28)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-o22xwekm-1634644004686)(output_12_2.png)]
def load_image(img_path):
im = Image.open(img_path).convert('L')
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.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)
ath)
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,
[[5.18793535]])
本次预测的数字是 [[5]]