# 使用LeNet在MNIST数据集实现图像分类¶

## 一、环境配置¶

[ ]:

import paddle

2.1.0


## 二、数据加载¶

[ ]:

from paddle.vision.transforms import Compose, Normalize

transform = Compose([Normalize(mean=[127.5],
std=[127.5],
data_format='CHW')])
# 使用transform对数据集做归一化

download training data and load training data

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

........

..

load finished


[ ]:

import numpy as np
import matplotlib.pyplot as plt
train_data0, train_label_0 = train_dataset[0][0],train_dataset[0][1]
train_data0 = train_data0.reshape([28,28])
plt.figure(figsize=(2,2))
plt.imshow(train_data0, cmap=plt.cm.binary)
print('train_data0 label is: ' + str(train_label_0))

train_data0 label is: [5]


## 三、组网¶

[ ]:

import paddle
def __init__(self):
super(LeNet, self).__init__()
self.conv2 = paddle.nn.Conv2D(in_channels=6, out_channels=16, kernel_size=5, stride=1)

def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.max_pool1(x)
x = F.relu(x)
x = self.conv2(x)
x = self.max_pool2(x)
x = self.linear1(x)
x = F.relu(x)
x = self.linear2(x)
x = F.relu(x)
x = self.linear3(x)
return x


## 四、方式1：基于高层API，完成模型的训练与预测¶

### 4.1 使用 Model.fit来训练模型¶

[ ]:

from paddle.metric import Accuracy

# 配置模型
model.prepare(
optim,
Accuracy()
)

[8]:

# 训练模型
model.fit(train_dataset,
epochs=2,
batch_size=64,
verbose=1
)

The loss value printed in the log is the current step, and the metric is the average value of previous steps.
Epoch 1/2
step 938/938 [==============================] - loss: 0.0025 - acc: 0.9564 - 9ms/step
Epoch 2/2
step 938/938 [==============================] - loss: 0.0127 - acc: 0.9844 - 9ms/step


### 4.2 使用 Model.evaluate 来预测模型¶

[9]:

model.evaluate(test_dataset, batch_size=64, verbose=1)

Eval begin...
step 157/157 [==============================] - loss: 1.2412e-04 - acc: 0.9872 - 8ms/step
Eval samples: 10000

[9]:

{'loss': [0.0001241174], 'acc': 0.9872}


## 五、方式2：基于基础API，完成模型的训练与预测¶

### 5.1 模型训练¶

[ ]:

import paddle.nn.functional as F
# 加载训练集 batch_size 设为 64
def train(model):
model.train()
epochs = 2
for epoch in range(epochs):
x_data = data[0]
y_data = data[1]
predicts = model(x_data)
loss = F.cross_entropy(predicts, y_data)
# 计算损失
loss.backward()
if batch_id % 300 == 0:
print("epoch: {}, batch_id: {}, loss is: {}, acc is: {}".format(epoch, batch_id, loss.numpy(), acc.numpy()))
optim.step()
model = LeNet()
train(model)

epoch: 0, batch_id: 0, loss is: [3.0527446], acc is: [0.09375]
epoch: 0, batch_id: 300, loss is: [0.05049332], acc is: [1.]
epoch: 0, batch_id: 600, loss is: [0.109704], acc is: [0.953125]


### 5.2 模型验证¶

[ ]:

test_loader = paddle.io.DataLoader(test_dataset, places=paddle.CPUPlace(), batch_size=64)
# 加载测试数据集
def test(model):
model.eval()
batch_size = 64
x_data = data[0]
y_data = data[1]
predicts = model(x_data)
# 获取预测结果
loss = F.cross_entropy(predicts, y_data)
if batch_id % 20 == 0:
print("batch_id: {}, loss is: {}, acc is: {}".format(batch_id, loss.numpy(), acc.numpy()))
test(model)

batch_id: 0, loss is: [0.01972857], acc is: [0.984375]
batch_id: 20, loss is: [0.19958115], acc is: [0.9375]
batch_id: 40, loss is: [0.23575728], acc is: [0.953125]
batch_id: 60, loss is: [0.07018849], acc is: [0.984375]
batch_id: 80, loss is: [0.02309197], acc is: [0.984375]
batch_id: 100, loss is: [0.00239462], acc is: [1.]
batch_id: 120, loss is: [0.01583934], acc is: [1.]
batch_id: 140, loss is: [0.00399609], acc is: [1.]