飞桨手写字体改进(分类)代码实现
时间: 2023-09-13 16:04:09 浏览: 103
利用k-近邻算法实现手写体分类代码及数据集
以下是使用飞桨框架实现手写字体分类改进的代码:
```python
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, Linear
# 定义 LeNet 网络结构
class LeNet(fluid.dygraph.Layer):
def __init__(self, num_classes=10):
super(LeNet, self).__init__()
# 第一个卷积层,输入图像大小为 28*28,输出图像大小为 24*24
self.conv1 = Conv2D(num_channels=1, num_filters=6, filter_size=5, act='sigmoid')
self.pool1 = Pool2D(pool_size=2, pool_stride=2, pool_type='max')
# 第二个卷积层,输入图像大小为 12*12,输出图像大小为 8*8
self.conv2 = Conv2D(num_channels=6, num_filters=16, filter_size=5, act='sigmoid')
self.pool2 = Pool2D(pool_size=2, pool_stride=2, pool_type='max')
# 全连接层,输入大小为 16*4*4,输出大小为 120
self.fc1 = Linear(input_dim=16*4*4, output_dim=120, act='sigmoid')
# 全连接层,输入大小为 120,输出大小为 84
self.fc2 = Linear(input_dim=120, output_dim=84, act='sigmoid')
# 全连接层,输入大小为 84,输出大小为 num_classes
self.fc3 = Linear(input_dim=84, output_dim=num_classes)
def forward(self, inputs):
x = self.conv1(inputs)
x = self.pool1(x)
x = self.conv2(x)
x = self.pool2(x)
x = fluid.layers.reshape(x, [x.shape[0], -1])
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
return x
# 定义训练函数
def train(model, optimizer, train_loader, epoch):
model.train()
for batch_id, data in enumerate(train_loader()):
x_data = data[0]
y_data = data[1]
img = fluid.dygraph.to_variable(x_data)
label = fluid.dygraph.to_variable(y_data)
logits = model(img)
loss = fluid.layers.softmax_with_cross_entropy(logits, label)
avg_loss = fluid.layers.mean(loss)
acc = fluid.layers.accuracy(logits, label)
if batch_id % 100 == 0:
print("epoch: {}, batch_id: {}, loss is: {}, acc is: {}".format(epoch, batch_id, avg_loss.numpy(), acc.numpy()))
avg_loss.backward()
optimizer.minimize(avg_loss)
model.clear_gradients()
# 定义测试函数
def test(model, test_loader):
model.eval()
accuracies = []
losses = []
for batch_id, data in enumerate(test_loader()):
x_data = data[0]
y_data = data[1]
img = fluid.dygraph.to_variable(x_data)
label = fluid.dygraph.to_variable(y_data)
logits = model(img)
loss = fluid.layers.softmax_with_cross_entropy(logits, label)
avg_loss = fluid.layers.mean(loss)
acc = fluid.layers.accuracy(logits, label)
accuracies.append(acc.numpy())
losses.append(avg_loss.numpy())
print("[validation] accuracy/loss: {}/{}".format(np.mean(accuracies), np.mean(losses)))
# 加载数据集
train_dataset = paddle.vision.datasets.MNIST(mode='train')
test_dataset = paddle.vision.datasets.MNIST(mode='test')
# 定义数据预处理函数
train_transforms = paddle.vision.transforms.Compose([
paddle.vision.transforms.Resize((32, 32)),
paddle.vision.transforms.Normalize(mean=[127.5], std=[127.5], data_format='CHW'),
paddle.vision.transforms.RandomHorizontalFlip(),
paddle.vision.transforms.RandomRotation(15),
paddle.vision.transforms.ToTensor()
])
test_transforms = paddle.vision.transforms.Compose([
paddle.vision.transforms.Resize((32, 32)),
paddle.vision.transforms.Normalize(mean=[127.5], std=[127.5], data_format='CHW'),
paddle.vision.transforms.ToTensor()
])
# 创建数据加载器
train_loader = paddle.io.DataLoader(train_dataset, batch_size=64, shuffle=True, drop_last=True, num_workers=2, use_shared_memory=True, collate_fn=None, return_list=False, transform=train_transforms)
test_loader = paddle.io.DataLoader(test_dataset, batch_size=64, shuffle=False, drop_last=True, num_workers=2, use_shared_memory=True, collate_fn=None, return_list=False, transform=test_transforms)
# 创建模型和优化器
model = LeNet(num_classes=10)
optimizer = fluid.optimizer.AdamOptimizer(learning_rate=0.001, parameter_list=model.parameters())
# 进行训练和测试
for epoch in range(10):
train(model, optimizer, train_loader, epoch)
test(model, test_loader)
```
在上述代码中,我们使用了飞桨内置的MNIST数据集,并使用了Compose函数定义了数据预处理函数。在训练中,我们使用了Adam优化器和交叉熵损失函数,同时打印了每个epoch中每个batch的平均loss和准确率。在测试中,我们只需计算模型预测的准确率和loss即可。
阅读全文