MindInsight:一款基于MindSpore框架的训练可视化插件
MindInsight:一款基于MindSpore框架的训练可视化插件
技术背景
在深度学习或者其他参数优化领域中,对于结果的可视化以及中间网络结构的可视化,也是一个非常重要的工作。一个好的可视化工具,可以更加直观的展示计算结果,可以帮助人们更快的发掘大量的数据中最有用的信息。而一般的深度学习框架或者是一些开源软件会支持这种可视化工具的接口。常见的可视化工具有TensorBoard和MindSpore的MindInsight,本文主要介绍MindInsight的安装与基本使用方法。
回到顶部
环境准备
MindInsight工具依赖于npm和nodejs,这里我们用Ubuntu的包管理工具apt来直接安装,首先需要更新apt的源和软件:
123456789101112131415161718192021(base) dechin@ubuntu2004:~/projects/gitee$ sudo apt update 获取:1 file:/var/nccl-repo-2.7.6-ga-cuda11.0 InRelease 忽略:1 file:/var/nccl-repo-2.7.6-ga-cuda11.0 InRelease 获取:2 file:/var/nccl-repo-2.7.6-ga-cuda11.0 Release [574 B] 获取:2 file:/var/nccl-repo-2.7.6-ga-cuda11.0 Release [574 B] 命中:3 https://nvidia.github.io/libnvidia-container/stable/ubuntu18.04/amd64 InRelease 命中:4 https://nvidia.github.io/nvidia-container-runtime/stable/ubuntu18.04/amd64 InRelease 命中:5 https://nvidia.github.io/nvidia-docker/ubuntu18.04/amd64 InRelease 命中:6 http://dl.google.com/linux/chrome/deb stable InRelease 命中:8 http://repo.huaweicloud.com/ubuntu focal InRelease 命中:10 http://repo.huaweicloud.com/ubuntu focal-updates InRelease 命中:11 http://repo.huaweicloud.com/ubuntu focal-backports InRelease 命中:12 http://repo.huaweicloud.com/ubuntu focal-security InRelease 获取:13 https://dl.cloudsmith.io/public/asbru-cm/release/deb/ubuntu focal InRelease [4,960 B] 忽略:9 https://developer.download.nvidia.cn/compute/machine-learning/repos/ubuntu1804/x86_64 InRelease 命中:14 https://developer.download.nvidia.cn/compute/machine-learning/repos/ubuntu1804/x86_64 Release 已下载 4,960 B,耗时 2秒 (2,625 B/s) 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 有 13 个软件包可以升级。请执行 ‘apt list --upgradable’ 来查看它们。
然后就可以直接用apt install
来安装,这里如果加上-y
的选项,在软件安装的过程中就不需要再手动的输入Y去确认是否要继续安装等:
1234567891011(base) dechin@ubuntu2004:~/projects/gitee$ sudo apt install -y nodejs npm 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 下列软件包是自动安装的并且现在不需要了: linux-headers-5.8.0-50-generic linux-hwe-5.8-headers-5.8.0-50 linux-image-5.8.0-50-generic linux-modules-5.8.0-50-generic linux-modules-extra-5.8.0-50-generic 使用'sudo apt autoremove'来卸载它(它们)。 将会同时安装下列软件: ...
安装完成后可以用查看版本号的指令验证两者是否被安装成功:
1234(base) dechin@ubuntu2004:~/projects/gitlab/dechin/src/mindspore$ nodejs --version v10.19.0 (base) dechin@ubuntu2004:~/projects/gitlab/dechin/src/mindspore$ npm --version 6.14.4
回到顶部
源码安装MindInsight
首先从gitee仓库下载源代码:
git clone https://gitee.com/mindspore/mindinsight.git
然后进入目录,使用pip递归的安装requirements中所要求的依赖包:
1234567(base) dechin@ubuntu2004:~/projects/gitee$ cd mindinsight/ (base) dechin@ubuntu2004:~/projects/gitee/mindinsight$ pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Requirement already satisfied: Click>=7.0 in /home/dechin/.local/lib/python3.8/site-packages (from -r requirements.txt (line 1)) (8.0.1) Requirement already satisfied: Flask>=1.1.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from -r requirements.txt (line 2)) (1.1.2) ... Successfully installed Flask-Cors-3.0.10 google-pasta-0.2.0 grpcio-1.36.0 gunicorn-20.1.0 marshmallow-3.12.1 treelib-1.6.1
最后在主目录下使用python setup.py install
的方式来安装这个python库:
1234567891011121314(base) dechin@ubuntu2004:~/projects/gitee/mindinsight$ python3 setup.py install running install running build running build_py creating build/lib/mindinsight copying mindinsight/__main__.py -> build/lib/mindinsight copying mindinsight/__init__.py -> build/lib/mindinsight copying mindinsight/_version.py -> build/lib/mindinsight running egg_info building ui ... > core-js@2.6.12 postinstall /home/dechin/projects/gitee/mindinsight/mindinsight/ui/node_modules/babel-runtime/node_modules/core-js > node -e "try{require('./postinstall')}catch(e){}"
回到顶部
基本使用与案例演示
MindInsight与我们所常用的python库有所不同,MindInsight更像是一种微服务,形式上与jupyter notebook有些类似。在使用前需要用mindinsight start
来启动服务,使用完成后,再使用mindinsight stop
来关闭服务:
1234567(base) dechin@ubuntu2004:~/projects/gitee/mindinsight$ mindinsight start Workspace: /home/dechin/mindinsight Summary base dir: /home/dechin/projects/gitee/mindinsight Web address: http://127.0.0.1:8080 service start state: success (base) dechin@ubuntu2004:~/projects/gitee/mindinsight$ mindinsight stop Stop mindinsight service successfully
这里我们来看一个案例,使用的案例是之前一篇博客中所用到的用线性神经网络拟合非线性函数的python代码:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859# test_nonlinear.pyfrom mindspore import context context.set_context(mode=context.GRAPH_MODE, device_target="CPU")import numpy as npfrom mindspore import dataset as dsfrom mindspore import nn, Tensor, Modelimport timefrom mindspore.train.callback import Callback, LossMonitor, SummaryCollectordef get_data(num, a=2.0, b=3.0): for _ in range(num): x = np.random.uniform(-1.0, 1.0) noise = np.random.normal(0, 0.03) z = a * x ** 2 + b + noise # 返回数据的时候就返回数据的平方 yield np.array([x**2]).astype(np.float32), np.array([z]).astype(np.float32)def create_dataset(num_data, batch_size=16, repeat_size=1): input_data = ds.GeneratorDataset(list(get_data(num_data)), column_names=['x','z']) input_data = input_data.batch(batch_size) input_data = input_data.repeat(repeat_size) return input_dataif __name__ == '__main__': data_number = 1600 # 一共产生1600组数据 batch_number = 16 # 分为16组分别进行优化 repeat_number = 2 # 重复2次,可以取到更低的损失函数值 ds_train = create_dataset(data_number, batch_size=batch_number, repeat_size=repeat_number) dict_datasets = next(ds_train.create_dict_iterator()) class LinearNet(nn.Cell): def __init__(self): super(LinearNet, self).__init__() self.fc = nn.Dense(1, 1, 0.02, 0.02) def construct(self, x): x = self.fc(x) return x net = LinearNet() model_params = net.trainable_params() print ('Param Shape is: {}'.format(len(model_params))) for net_param in net.trainable_params(): print(net_param, net_param.asnumpy()) net_loss = nn.loss.MSELoss() # 设定优化算法,常用的是Momentum和ADAM optim = nn.Momentum(net.trainable_params(), learning_rate=0.005, momentum=0.9) model = Model(net, net_loss, optim) epoch = 1 # 设定每8个batch训练完成后就播报一次,这里一共播报25次 summary_collector = SummaryCollector(summary_dir='./summary_dir') model.train(epoch, ds_train, callbacks=[LossMonitor(8), summary_collector], dataset_sink_mode=False) for net_param in net.trainable_params(): print(net_param, net_param.asnumpy())
因为要使用MindInsight来展示结果,我们所需要执行的更改,只需要增加一个SummaryCollector的callback即可。执行上述python代码之后,可以看到在summary_dir目录下生成了两个文件:
123456dechin@ubuntu2004:~/projects/gitlab/dechin/src/mindspore$ ll summary_dir/ 总用量 24 drwxrwxr-x 2 dechin dechin 4096 7月 6 10:48 ./ drwxrwxr-x 7 dechin dechin 4096 7月 6 10:47 ../ -r-------- 1 dechin dechin 319 7月 6 10:48 events.out.events.summary.1625539702.0.ubuntu2004_lineage -r-------- 1 dechin dechin 10064 7月 6 10:48 events.out.events.summary.1625539702.0.ubuntu2004_MS
最后在浏览器中输入http://127.0.0.1:8080
即可打开训练数据的可视化面板:
包含有中间训练的Loss Function趋势:
还有参数配置详情等:
回到顶部
总结概要
可视化可以说在所有的研究领域中都是要仔细斟酌的问题,一个好的可视化工具不仅可以为工作的开展带来极大的便捷,在一些特殊的场景下还可以辅助人们进行一些重要的决策。比如在物理学领域中,最开始寻找相变点的技巧其实也是依赖于实验数据的可视化来实现的。而在深度学习领域,可视化模型、可视化训练过程、可视化参数以及可视化的总结,可以让我们迅速的得到模型好坏的结论,这一点我们在本文中通过研究MindInsight的一些安装与使用技巧来进行实现。
版权声明
本文首发链接为:https://www.cnblogs.com/dechinphy/p/msinsight.html
作者ID:DechinPhy
来源https://www.cnblogs.com/dechinphy/p/msinsight.html