动转静部署
import paddle
# 定义手写数字识别模型
class MNIST(paddle.nn.Layer):
def __init__(self):
super(MNIST, self).__init__()
# 定义一层全连接层,输出维度是1
self.fc = paddle.nn.Linear(in_features=784, out_features=10)
# 定义网络结构的前向计算过程
@paddle.jit.to_static # 添加装饰器,使动态图网络结构在静态图模式下运行
def forward(self, inputs):
outputs = self.fc(inputs)
return outputs
import paddle
import paddle.nn.functional as F
# 确保从paddle.vision.datasets.MNIST中加载的图像数据是np.ndarray类型
paddle.vision.set_image_backend('cv2')
# 图像归一化函数,将数据范围为[0, 255]的图像归一化到[-1, 1]
def norm_img(img):
batch_size = img.shape[0]
# 归一化图像数据
img = img / 127.5 - 1
# 将图像形式reshape为[batch_size, 784]
img = paddle.reshape(img, [batch_size, 784])
return img
def train(model):
model.train()
# 加载训练集 batch_size 设为 16
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('int64')
# 前向计算的过程
predicts = model(images)
# 计算损失
loss = F.cross_entropy(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()
model = MNIST()
train(model)
paddle.save(model.state_dict(), './mnist.pdparams')
print("==>Trained model saved in ./mnist.pdparams")
# save inference model
from paddle.static import InputSpec
# 加载训练好的模型参数
state_dict = paddle.load("./mnist.pdparams")
# 将训练好的参数读取到网络中
model.set_state_dict(state_dict)
# 设置模型为评估模式
model.eval()
# 保存inference模型
paddle.jit.save(
layer=model,
path="inference/mnist",
input_spec=[InputSpec(shape=[None, 784], dtype='float32')])
print("==>Inference model saved in inference/mnist.")
import numpy as np
import paddle
import paddle.nn.functional as F
# 确保从paddle.vision.datasets.MNIST中加载的图像数据是np.ndarray类型
paddle.vision.set_image_backend('cv2')
# 读取mnist测试数据,获取第一个数据
mnist_test = paddle.vision.datasets.MNIST(mode='test')
test_image, label = mnist_test[0]
# 获取读取到的图像的数字标签
print("The label of readed image is : ", label)
# 将测试图像数据转换为tensor,并reshape为[1, 784]
test_image = paddle.reshape(paddle.to_tensor(test_image), [1, 784])
# 然后执行图像归一化
test_image = norm_img(test_image)
# 加载保存的模型
loaded_model = paddle.jit.load("./inference/mnist")
# 利用加载的模型执行预测
preds = loaded_model(test_image)
pred_label = paddle.argmax(preds)
# 打印预测结果
print("The predicted label is : ", pred_label.numpy())
结果如下:
DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working
return (isinstance(seq, collections.Sequence) and
epoch_id: 0, batch_id: 0, loss is: [2.7604132]
epoch_id: 0, batch_id: 1000, loss is: [0.96179533]
epoch_id: 0, batch_id: 2000, loss is: [0.53986955]
epoch_id: 0, batch_id: 3000, loss is: [0.5623173]
epoch_id: 1, batch_id: 0, loss is: [0.6740942]
epoch_id: 1, batch_id: 1000, loss is: [0.8340612]
epoch_id: 1, batch_id: 2000, loss is: [0.5981833]
epoch_id: 1, batch_id: 3000, loss is: [0.4298735]
epoch_id: 2, batch_id: 0, loss is: [0.28526694]
epoch_id: 2, batch_id: 1000, loss is: [0.3931272]
epoch_id: 2, batch_id: 2000, loss is: [0.3575241]
epoch_id: 2, batch_id: 3000, loss is: [0.40944722]
epoch_id: 3, batch_id: 0, loss is: [0.22089666]
epoch_id: 3, batch_id: 1000, loss is: [0.57547796]
epoch_id: 3, batch_id: 2000, loss is: [0.5900006]
epoch_id: 3, batch_id: 3000, loss is: [0.38643128]
epoch_id: 4, batch_id: 0, loss is: [0.26384747]
epoch_id: 4, batch_id: 1000, loss is: [0.08868149]
epoch_id: 4, batch_id: 2000, loss is: [0.10807225]
epoch_id: 4, batch_id: 3000, loss is: [0.42670283]
epoch_id: 5, batch_id: 0, loss is: [0.15351322]
epoch_id: 5, batch_id: 1000, loss is: [0.81782436]
epoch_id: 5, batch_id: 2000, loss is: [0.63281745]
epoch_id: 5, batch_id: 3000, loss is: [0.3347082]
epoch_id: 6, batch_id: 0, loss is: [0.35385633]
epoch_id: 6, batch_id: 1000, loss is: [0.46075183]
epoch_id: 6, batch_id: 2000, loss is: [0.5838824]
epoch_id: 6, batch_id: 3000, loss is: [0.5748338]
epoch_id: 7, batch_id: 0, loss is: [0.7031684]
epoch_id: 7, batch_id: 1000, loss is: [0.5323996]
epoch_id: 7, batch_id: 2000, loss is: [0.03418297]
epoch_id: 7, batch_id: 3000, loss is: [0.22748059]
epoch_id: 8, batch_id: 0, loss is: [0.1814915]
epoch_id: 8, batch_id: 1000, loss is: [0.27604705]
epoch_id: 8, batch_id: 2000, loss is: [0.2034478]
epoch_id: 8, batch_id: 3000, loss is: [0.29277503]
epoch_id: 9, batch_id: 0, loss is: [0.15433009]
epoch_id: 9, batch_id: 1000, loss is: [0.31987372]
epoch_id: 9, batch_id: 2000, loss is: [0.23542832]
epoch_id: 9, batch_id: 3000, loss is: [0.6694958]
==>Trained model saved in ./mnist.pdparams
C:\360Downloads\Software\Anaconda\Anaconda\lib\site-packages\paddle\fluid\dygraph\jit.py:412: UserWarning: The InputSpec(shape=(-1, 784), dtype=VarType.FP32, name=None)'s name is None. When using jit.save, please set InputSepc's name in to_static(input_spec=[]) and jit.save(input_spec=[]) and make sure they are consistent.
warnings.warn(name_none_error % spec)
==>Inference model saved in inference/mnist.
Cache file C:\Users\80708\.cache\paddle\dataset\mnist\t10k-images-idx3-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/t10k-images-idx3-ubyte.gz
Begin to download
Download finished
Cache file C:\Users\80708\.cache\paddle\dataset\mnist\t10k-labels-idx1-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/t10k-labels-idx1-ubyte.gz
Begin to download
..
Download finished
The label of readed image is : [7]
The predicted label is : [7]
Process finished with exit code 0