2020年3月28日,MindSpore正式开源,备受广大开发者的关注。从4月30日发布的v0.2.0-alpha版本,到8月30日发布的v0.7.0-beta版本,推出了数十个亮眼的新特性,9月,在华为全联接大会上,MindSpore正式发布v1.0.0版本。
有关Mindspore 1.0更多的介绍可以看这一篇文章:https://www.mindspore.cn/news/newschildren?id=262
Mindspore安装教程参考文档:https://www.mindspore.cn/install
这边有个小建议:国内通过pip install 安装的用户可以在教程页面给出的安装命令的最后加上 -i https://mirrors.huaweicloud.com/repository/pypi/simple 命令使用国内源。因为在安装mindspore的时候会安装一些依赖包,这些依赖包没制定过安装源的话他会从国外的默认地址下载,那样的话在国内下载可能比较慢。所有建议加上-i 指定一下国内源。
如果想要使用docker安装的话可以参考这个文档:https://gitee.com/mindspore/mindspore 中的Docer镜像部分安装。
硬件环境:
CPU:Intel i7-4712MQ
内存:8G
显卡:Nvidia GeForce 840M
软件环境:
操作系统:WIN10 2004
Python:Python 3.7.7
根据官方安装教程安装完毕后,在命令提示符输入python,然后输入代码,执行结果显示1.0.0说明Mindspore 1.0.0安装成功了。
12 | import mindspore as msms.__version__ |
三、使用Lenet+Mnist初体验
LeNet-5是一个较简单的卷积神经网络。下图显示了其结构:输入的二维图像,先经过两次卷积层到池化层,再经过全连接层,最后使用softmax分类作为输出层。
LeNet-5 这个网络虽然很小,但是它包含了深度学习的基本模块:卷积层,池化层,全连接层。是其他深度学习模型的基础, 这里我们对LeNet-5进行深入分析。同时,通过实例分析,加深对与卷积层和池化层的理解。
MNIST 数据集已经是一个被”嚼烂”了的数据集, 很多教程都会对它”下手”, 几乎成为一个 “典范”. 不过有些人可能对它还不是很了解, 下面来介绍一下.
MNIST 数据集可在http://yann.lecun.com/exdb/mnist/获取, 它包含了四个部分:
训练集:
Training set images: train-images-idx3-ubyte.gz (9.9 MB, 解压后 47 MB, 包含 60,000 个样本)
Training set labels: train-labels-idx1-ubyte.gz (29 KB, 解压后 60 KB, 包含 60,000 个标签)
测试集
Test set images: t10k-images-idx3-ubyte.gz (1.6 MB, 解压后 7.8 MB, 包含 10,000 个样本)
Test set labels: t10k-labels-idx1-ubyte.gz (5KB, 解压后 10 KB, 包含 10,000 个标签)
MNIST 数据集来自美国国家标准与技术研究所, National Institute of Standards and Technology (NIST). 训练集 (training set) 由来自 250 个不同人手写的数字构成, 其中 50% 是高中学生, 50% 来自人口普查局 (the Census Bureau) 的工作人员. 测试集(test set) 也是同样比例的手写数字数据.
程序运行代码可以参考Mindspore官方代码仓:https://gitee.com/mindspore/mindspore/tree/r1.0/model_zoo/official/cv/lenet
第一步:将代码下载下来拷贝到本地,代码的文件夹结构如下
lenet
|___src
| |__config.py
| |__dataset.py
| |__lenet.py
|
|___MNIST_DATA
| |______train(MNIST下载后的训练集解压到这个目录
| |______test(MNIST下载后的验证集解压到这个目录
|
|____train.py 训练代码
|____eval.py 测试代码
train.py的代码:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 | # Copyright 2020 Huawei Technologies Co., Ltd## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.# ============================================================================"""######################## train lenet example ########################train lenet and get network model files(.ckpt) :python train.py --data_path /YourDataPath""" import osimport astimport argparsefrom src.config import mnist_cfg as cfgfrom src.dataset import create_datasetfrom src.lenet import LeNet5import mindspore.nn as nnfrom mindspore import contextfrom mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitorfrom mindspore.train import Modelfrom mindspore.nn.metrics import Accuracyfrom mindspore.common import set_seed set_seed(1) if __name__ == "__main__": parser = argparse.ArgumentParser(description='MindSpore Lenet Example') parser.add_argument('--device_target', type=str, default="CPU", choices=['Ascend', 'GPU', 'CPU'], help='device where the code will be implemented (default: Ascend)') parser.add_argument('--data_path', type=str, default="./MNIST_Data", help='path where the dataset is saved') parser.add_argument('--ckpt_path', type=str, default="./ckpt", help='if is test, must provide\ path where the trained ckpt file') parser.add_argument('--dataset_sink_mode', type=ast.literal_eval, default=True, help='dataset_sink_mode is False or True') args = parser.parse_args() context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target) ds_train = create_dataset(os.path.join(args.data_path, "train"), cfg.batch_size) network = LeNet5(cfg.num_classes) net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum) time_cb = TimeMonitor(data_size=ds_train.get_dataset_size()) config_ck = CheckpointConfig(save_checkpoint_steps=cfg.save_checkpoint_steps, keep_checkpoint_max=cfg.keep_checkpoint_max) ckpoint_cb = ModelCheckpoint(prefix="checkpoint_lenet", directory=args.ckpt_path, config=config_ck) model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()}) print("============== Starting Training ==============") model.train(cfg['epoch_size'], ds_train, callbacks=[time_cb, ckpoint_cb, LossMonitor()], dataset_sink_mode=args.dataset_sink_mode) |
eval.py的代码:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 | # Copyright 2020 Huawei Technologies Co., Ltd## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.# ============================================================================"""######################## eval lenet example ########################eval lenet according to model file:python eval.py --data_path /YourDataPath --ckpt_path Your.ckpt""" import osimport astimport argparseimport mindspore.nn as nnfrom mindspore import contextfrom mindspore.train.serialization import load_checkpoint, load_param_into_netfrom mindspore.train import Modelfrom mindspore.nn.metrics import Accuracyfrom src.dataset import create_datasetfrom src.config import mnist_cfg as cfgfrom src.lenet import LeNet5 if __name__ == "__main__": parser = argparse.ArgumentParser(description='MindSpore Lenet Example') parser.add_argument('--device_target', type=str, default="CPU", choices=['Ascend', 'GPU', 'CPU'], help='device where the code will be implemented (default: Ascend)') parser.add_argument('--data_path', type=str, default="./MNIST_Data", help='path where the dataset is saved') parser.add_argument('--ckpt_path', type=str, default="", help='if mode is test, must provide\ path where the trained ckpt file') parser.add_argument('--dataset_sink_mode', type=ast.literal_eval, default=False, help='dataset_sink_mode is False or True') args = parser.parse_args() context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target) network = LeNet5(cfg.num_classes) net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") repeat_size = cfg.epoch_size net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum) model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()}) print("============== Starting Testing ==============") param_dict = load_checkpoint(args.ckpt_path) load_param_into_net(network, param_dict) ds_eval = create_dataset(os.path.join(args.data_path, "test"), cfg.batch_size, 1) acc = model.eval(ds_eval, dataset_sink_mode=args.dataset_sink_mode) print("============== {} ==============".format(acc)) |
src目录下config.py(用来设置运行参数的文件)
1234567891011121314 | from easydict import EasyDict as edict mnist_cfg = edict({ 'num_classes': 10, 'lr': 0.01, 'momentum': 0.9, 'epoch_size': 10, 'batch_size': 32, 'buffer_size': 1000, 'image_height': 32, 'image_width': 32, 'save_checkpoint_steps': 1875, 'keep_checkpoint_max': 10,}) |
第二步:执行train.py开始训练模型的
命令:python train.py
执行结果:
看到出现Epoch time:和per step time且屏幕不再滚动就说明训练已经完成。训练完成后我们可以发现代码目录下生成了一个新的文件夹叫CKPT,这个文件夹内存放的就是训练后的ckpt文件,在后面的验证代码中会用到这个里面的文件
第三步:执行验证代码test.py
命令:python eval.py --ckpt_path=./ckpt/checkpoint_lenet-10_1875.ckpt
说明:--ckpt_path 参数指定ckpt文件路径。这里我们选择前面训练产生的最后一个ckpt,也就是checkpoint_lenet-10_1875.ckpt
执行结果:
看到命令行中出现'Accuracy'的结果后,就说明代码运行成功了,可以看到这次模型训练的进度为98.4375%还算可以。
Lenet+MNIST的模型训练在我看来可以算是Mindspore的Hello world了,执行起来难度不是很大,整个的运行时间也不会很久。这次的模型训练是在CPU环境下运行的,等后面有机会装一个GPU版本,跑一个resnet50+Cifar10看看效果有没有比前几个版本的运行速度快一点。当然更多的算法实例可以参考Mindspore官方码云下的Model_Zoo,里面还有不少好玩的算法代码。有兴趣的可以去试着跑跑看,代码地址:https://gitee.com/mindspore/mindspore/tree/r1.0/model_zoo
转自文章链接:Mindspore 1.0初体验_MindSpore_昇腾论坛_华为云论坛
感谢作者的努力与分享,侵权立删!