使用华为云Ascend910在MNIST上面训练LeNet网络(ModelArts版)
【摘要】 1 使用华为云Ascend910在MNIST上面训练LeNet网络实验题(需要申请环境):使用华为云Ascend910在MNIST上面训练LeNet网络,上传loss截图和推理精度截图MindSpore官网:https://www.mindspore.cn/开源地址:https://gitee.com/mindspore课程案例:https://gitee.com/mindspore/co...
1 使用华为云Ascend910在MNIST上面训练LeNet网络
实验题(需要申请环境):使用华为云Ascend910在MNIST上面训练LeNet网络,上传loss截图和推理精度截图
-
MindSpore官网:https://www.mindspore.cn/
1.1 LeNet
Paper: Y. LeCun, L. Bottou, Y. Bengio, and P. Haffner. “Gradient-based learning applied to document recognition.” Proceedings of the IEEE, 86(11):2278-2324, November 1998 https://ieeexplore.ieee.org/document/726791
MindSpore实现:https://gitee.com/mindspore/course/tree/master/lenet5
LeNet 结构
1.2 数据集下载
- 方式一:下载源数据
- 方式二:ModelArts + OBS (推荐)
1)创建桶:华北-北京四、单AZ存储、同名称、其他默认或自定义
2)将下载的数据集上传OBS ( MNIST)
2.3 创建ModelArts 开始训练
(1)创建Notebook
- ModelArts–开发环境–Noteboot–创建(按需计费、名称、公共镜像->Ascend-Powered-Engine 1.0 (Python3)、对象存储服务(OBS)-> MNIST)
(2)进入Jupyter 或者jpyter Lab
- 新建 Mindspore-python3.7 lnet.ipynb
- ModelArts 与OBS 数据集拷贝
# 拷贝自己账户下OBS桶内的数据集至执行容器
import moxing
moxing.file.copy_parallel(src_url="s3://sunxiaobei/MNIST/", dst_url='MNIST/')
# src_url形如's3://OBS/PATH',为OBS桶中数据集的路径,dst_url为执行容器中的路径
# 拷贝共享的OBS桶内的数据集至执行容器
import moxing
moxing.file.copy_parallel(src_url="s3://share-course/dataset/MNIST/", dst_url='MNIST/')
- 导入模块
import os
# os.environ['DEVICE_ID'] = '0'
import mindspore as ms
import mindspore.context as context
import mindspore.dataset.transforms.c_transforms as C
import mindspore.dataset.vision.c_transforms as CV
from mindspore import nn
from mindspore.train import Model
from mindspore.train.callback import LossMonitor
context.set_context(mode=context.GRAPH_MODE, device_target='Ascend') # Ascend, CPU, GPU
- 数据处理
def create_dataset(data_dir, training=True, batch_size=32, resize=(32, 32),
rescale=1/(255*0.3081), shift=-0.1307/0.3081, buffer_size=64):
data_train = os.path.join(data_dir, 'train') # train set
data_test = os.path.join(data_dir, 'test') # test set
ds = ms.dataset.MnistDataset(data_train if training else data_test)
ds = ds.map(input_columns=["image"], operations=[CV.Resize(resize), CV.Rescale(rescale, shift), CV.HWC2CHW()])
ds = ds.map(input_columns=["label"], operations=C.TypeCast(ms.int32))
# When `dataset_sink_mode=True` on Ascend, append `ds = ds.repeat(num_epochs) to the end
ds = ds.shuffle(buffer_size=buffer_size).batch(batch_size, drop_remainder=True)
return ds
# 可视化,图片的大小为32x32
import matplotlib.pyplot as plt
ds = create_dataset('MNIST', training=False)
data = ds.create_dict_iterator(output_numpy=True).get_next()
images = data['image']
labels = data['label']
for i in range(1, 5):
plt.subplot(2, 2, i)
plt.imshow(images[i][0])
plt.title('Number: %s' % labels[i])
plt.xticks([])
plt.show()
- 定义模型
模型结构如下图所示:
class LeNet5(nn.Cell):
def __init__(self):
super(LeNet5, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5, stride=1, pad_mode='valid')
self.conv2 = nn.Conv2d(6, 16, 5, stride=1, pad_mode='valid')
self.relu = nn.ReLU()
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.flatten = nn.Flatten()
self.fc1 = nn.Dense(400, 120)
self.fc2 = nn.Dense(120, 84)
self.fc3 = nn.Dense(84, 10)
def construct(self, x):
x = self.relu(self.conv1(x))
x = self.pool(x)
x = self.relu(self.conv2(x))
x = self.pool(x)
x = self.flatten(x)
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
return x
- 训练
# - batch size :32
# - number of epochs :3
# - learning rate : 0.01
# - optimizer: Momentum 0.9
def train(data_dir, lr=0.01, momentum=0.9, num_epochs=3):
ds_train = create_dataset(data_dir)
ds_eval = create_dataset(data_dir, training=False)
net = LeNet5()
loss = nn.loss.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
opt = nn.Momentum(net.trainable_params(), lr, momentum)
loss_cb = LossMonitor(per_print_times=ds_train.get_dataset_size())
model = Model(net, loss, opt, metrics={'acc', 'loss'})
# dataset_sink_mode can be True when using Ascend
model.train(num_epochs, ds_train, callbacks=[loss_cb], dataset_sink_mode=False)
metrics = model.eval(ds_eval, dataset_sink_mode=False)
print('Metrics:', metrics)
train('MNIST/')
- 训练结果
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)