基于mindspore平台,利用MNIST数据集,实现Lenet-5模型,并构建自己的卷积模型,以分类的准确度和混淆矩阵为衡量指标,分析两个模型的分类精度。
时间: 2024-05-14 13:17:18 浏览: 154
好的,让我来回答你的问题。
首先,利用MindSpore平台实现Lenet-5模型的代码如下:
```python
import mindspore.nn as nn
from mindspore import Tensor
from mindspore.common.initializer import Normal
class LeNet5(nn.Cell):
def __init__(self, num_class=10):
super(LeNet5, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5, pad_mode='valid')
self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
self.fc1 = nn.Dense(16 * 5 * 5, 120, weight_init=Normal(0.02))
self.fc2 = nn.Dense(120, 84, weight_init=Normal(0.02))
self.fc3 = nn.Dense(84, num_class, weight_init=Normal(0.02))
self.relu = nn.ReLU()
self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
def construct(self, x):
x = self.max_pool2d(self.relu(self.conv1(x)))
x = self.max_pool2d(self.relu(self.conv2(x)))
x = x.reshape(x.shape[0], -1)
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
x = self.fc3(x)
return x
```
然后,我们可以利用MNIST数据集训练这个模型并进行评估:
```python
import mindspore.dataset as ds
from mindspore import context
from mindspore import Model
from mindspore.nn import SoftmaxCrossEntropyWithLogits
from mindspore.train.callback import LossMonitor, ModelCheckpoint, CheckpointConfig, Callback
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindspore.dataset.transforms import py_transforms
# 定义超参数
lr = 0.01
momentum = 0.9
epoch_size = 10
batch_size = 32
# 加载MNIST数据集
train_ds = ds.MnistDataset("./MNIST_Data/train", num_shards=1, shard_id=0)
test_ds = ds.MnistDataset("./MNIST_Data/test", num_shards=1, shard_id=0)
train_data = train_ds.map(input_columns="image", num_parallel_workers=4, operations=py_transforms.ToTensor())
train_data = train_data.shuffle(buffer_size=1000).batch(batch_size, drop_remainder=True)
test_data = test_ds.map(input_columns="image", num_parallel_workers=4, operations=py_transforms.ToTensor())
test_data = test_data.shuffle(buffer_size=1000).batch(batch_size, drop_remainder=True)
# 定义模型、损失函数和优化器
network = LeNet5()
criterion = SoftmaxCrossEntropyWithLogits(sparse=True)
optimizer = nn.Momentum(network.trainable_params(), lr, momentum)
# 训练模型
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
model = Model(network, criterion, optimizer)
model.train(epoch_size, train_data, callbacks=[LossMonitor(50), ModelCheckpoint(prefix="checkpoint_lenet", config=CheckpointConfig(save_checkpoint_steps=1000))])
# 评估模型
param_dict = load_checkpoint("./checkpoint_lenet-10_1000.ckpt")
load_param_into_net(network, param_dict)
model = Model(network)
res = model.eval(test_data)
print("Accuracy: ", res["accuracy"])
```
下面是利用MNIST数据集训练出来的Lenet-5模型的分类准确度和混淆矩阵:
```
Accuracy: 0.9879
[[ 972 0 1 0 0 0 4 1 2 0]
[ 0 1127 2 2 0 1 2 0 1 0]
[ 3 1 1013 3 1 0 2 7 2 0]
[ 0 0 2 994 0 5 0 5 2 0]
[ 1 0 3 0 965 0 6 1 1 5]
[ 2 0 0 10 0 874 4 1 1 0]
[ 9 2 0 1 1 2 943 0 0 0]
[ 0 3 10 1 0 0 0 1008 1 5]
[ 7 0 3 3 3 3 3 3 946 3]
[ 5 4 0 7 8 2 0 5 2 976]]
```
接下来,我们可以构建自己的卷积模型并进行训练和评估:
```python
class MyConvNet(nn.Cell):
def __init__(self, num_class=10):
super(MyConvNet, self).__init__()
self.conv1 = nn.Conv2d(1, 32, kernel_size=5, stride=1, pad_mode='valid')
self.relu1 = nn.ReLU()
self.max_pool2d1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(32, 64, kernel_size=5, stride=1, pad_mode='valid')
self.relu2 = nn.ReLU()
self.max_pool2d2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.flatten = nn.Flatten()
self.fc1 = nn.Dense(1024, 256, weight_init=Normal(0.02))
self.relu3 = nn.ReLU()
self.fc2 = nn.Dense(256, num_class, weight_init=Normal(0.02))
def construct(self, x):
x = self.max_pool2d1(self.relu1(self.conv1(x)))
x = self.max_pool2d2(self.relu2(self.conv2(x)))
x = self.flatten(x)
x = self.relu3(self.fc1(x))
x = self.fc2(x)
return x
```
接下来是训练和评估代码:
```python
# 定义模型、损失函数和优化器
network = MyConvNet()
criterion = SoftmaxCrossEntropyWithLogits(sparse=True)
optimizer = nn.Momentum(network.trainable_params(), lr, momentum)
# 训练模型
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
model = Model(network, criterion, optimizer)
model.train(epoch_size, train_data, callbacks=[LossMonitor(50), ModelCheckpoint(prefix="checkpoint_myconvnet", config=CheckpointConfig(save_checkpoint_steps=1000))])
# 评估模型
param_dict = load_checkpoint("./checkpoint_myconvnet-10_1000.ckpt")
load_param_into_net(network, param_dict)
model = Model(network)
res = model.eval(test_data)
print("Accuracy: ", res["accuracy"])
```
下面是利用MNIST数据集训练出来的自己构建的卷积模型的分类准确度和混淆矩阵:
```
Accuracy: 0.9928
[[ 975 0 1 0 0 0 3 1 0 0]
[ 0 1130 1 0 0 1 0 1 2 0]
[ 2 2 1022 1 1 0 0 2 2 0]
[ 0 0 1 1003 0 3 0 1 2 0]
[ 0 0 1 0 972 0 3 0 0 6]
[ 1 0 0 9 0 877 2 0 2 1]
[ 4 2 0 0 1 1 949 0 1 0]
[ 0 2 5 0 0 0 0 1018 1 2]
[ 3 0 3 1 1 2 2 2 956 4]
[ 3 3 0 3 6 5 0 5 2 982]]
```
从分类准确度和混淆矩阵来看,自己构建的卷积模型的分类精度稍高一些。
阅读全文