mindspore手写识别代码
时间: 2023-09-13 15:13:59 浏览: 116
以下是使用MindSpore实现手写数字识别的简单代码:
```python
import numpy as np
import mindspore.dataset as ds
import mindspore.dataset.transforms.c_transforms as C
import mindspore.dataset.vision.c_transforms as CV
import mindspore.dataset.transforms.py_transforms as pyC
import mindspore.nn as nn
from mindspore import context, Tensor
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor
from mindspore.train.model import Model
from mindspore.nn.metrics import Accuracy
from mindspore.common.initializer import Normal
# 加载数据集
def create_dataset(data_path, batch_size=32, repeat_size=1,
num_parallel_workers=1):
# 定义图片转换操作
trans = []
trans.append(CV.Resize((32, 32)))
trans.append(CV.RandomCrop((28, 28)))
trans.append(CV.Rescale(1 / 255.0, 0))
trans.append(C.Reshape([-1]))
type_cast_op = pyC.TypeCast(np.int32)
trans = C.Compose(trans)
# 加载数据集
mnist_ds = ds.MnistDataset(data_path, shuffle=True)
# 应用转换操作
mnist_ds = mnist_ds.map(operations=trans, input_columns="image",
num_parallel_workers=num_parallel_workers)
mnist_ds = mnist_ds.map(operations=type_cast_op, input_columns="label",
num_parallel_workers=num_parallel_workers)
# 对数据集进行批处理、重复和预取
mnist_ds = mnist_ds.batch(batch_size=batch_size, drop_remainder=True)
mnist_ds = mnist_ds.repeat(repeat_size)
mnist_ds = mnist_ds.prefetch(buffer_size=num_parallel_workers)
return mnist_ds
# 定义模型
class LeNet5(nn.Cell):
def __init__(self):
super(LeNet5, self).__init__()
self.conv1 = nn.Conv2d(1, 6, kernel_size=5, pad_mode='valid')
self.conv2 = nn.Conv2d(6, 16, kernel_size=5, pad_mode='valid')
self.fc1 = nn.Dense(16 * 4 * 4, 120, weight_init=Normal(0.02))
self.fc2 = nn.Dense(120, 84, weight_init=Normal(0.02))
self.fc3 = nn.Dense(84, 10, 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.conv1(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = self.conv2(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = nn.Flatten()(x)
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.relu(x)
x = self.fc3(x)
return x
if __name__ == '__main__':
# 设置设备环境为CPU
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
# 加载数据集
data_path = "./MNIST_unzip/train"
mnist_ds = create_dataset(data_path)
# 定义模型
network = LeNet5()
# 定义损失函数和优化器
loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
opt = nn.Momentum(network.trainable_params(), learning_rate=0.01, momentum=0.9)
# 定义模型训练和评估
model = Model(network, loss_fn=loss, optimizer=opt, metrics={"Accuracy": Accuracy()})
# 定义回调函数
ckpt_config = CheckpointConfig(save_checkpoint_steps=1875, keep_checkpoint_max=10)
ckpt_cb = ModelCheckpoint(prefix="checkpoint_lenet", config=ckpt_config)
# 开始训练
model.train(epoch=1, train_dataset=mnist_ds, callbacks=[ckpt_cb, LossMonitor()], dataset_sink_mode=False)
```
以上代码使用MindSpore实现了一个简单的LeNet-5模型,用于识别手写数字。代码中使用了MindSpore提供的数据集加载、数据增强、模型定义、损失函数、优化器、回调函数等功能,同时对代码进行了解释,可供初学者参考学习。
阅读全文