华为AI框架:MindSpore+MindInsight安装-Ubuntu

MindSpore+MindInsight安装-Ubuntu

MindSpore介绍和安装缘由

MindSpore是华为开源自研AI框架,类似于Pytorch和Tensorflow。因为任务需求,需要将代码转换为MindSpore的版本,故学习和使用。由于现在还在发展中,这个框架还存在一些问题。在安装中我就遇到了一些问题,记录下来希望对其他人有所帮助。

###安装MindSpore

在尝试多种安装方式后,找到了快捷方便的方式。命令如下:

pip install --upgrade mindspore-gpu

这个会安装最新且为GPU版本的MindSpore

如我的安装后的版本信息:

mindspore_gpu-1.2.0-cp38-cp38-manylinux1_x86_64.whl

官网给出的其他安装方式,其中pip方式我安装了没有成功,采用上述我提供的方式就会简单很多。

验证是否成功安装

由于官网给的测试代码是为了推自研的人工智能芯片Ascend,所以提供的测试代码需要修改一行,才符合我们一般的使用者。

测试代码如下:

import numpy as np
from mindspore import Tensor
import mindspore.ops as ops
import mindspore.context as context

context.set_context(device_target="GPU")#这里不同,改成GPU了,表示使用GPU。如果是用CPU,这里改成“CPU”即可
x = Tensor(np.ones([1,3,3,4]).astype(np.float32))
y = Tensor(np.ones([1,3,3,4]).astype(np.float32))
print(ops.add(x, y))

结果是值全为2的矩阵,矩阵shape是[1,3,3,4]

安装MindInsight

  • MindInsight为MindSpore提供了简单易用的调优调试能力。在训练过程中,可以将标量、张量、图像、计算图、模型超参、训练耗时等数据记录到文件中,通过MindInsight可视化页面进行查看及分析。

  • 安装MindInsight其实是为了使用MindConverter(为MindInsight的子模块)。MindConverter可以将PyTorch、TensorFlow脚本转换到MindSpore脚本的工具。安装过程中依旧遇到了很多问题,希望我的解决方案能够帮助他人。

官网给出的安装方式。其中pip方式依旧拉垮。可能是我设置不对,反正多种尝试后,均不行。后采用源码编译的方式安装。

从代码仓下载源码
git clone https://gitee.com/mindspore/mindinsight.git

编译和安装我采用以下方式

cd mindinsight
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
python setup.py install

遇到的问题

  • Cmake更新,Cmake版本过低问题

    pip install cmake --upgrade
    

    这样就安装到最新版了。

  • npm报错,会有各种报错。经过测试发现就是版本的问题或者npm没有安装。

    sudo apt-get install npm
    npm install -g npm@4.0.0
    

    第一行是安装npm,第二行是调整npm的版本。我的MindInsight是1.2版本,经过测试npm是4.0.0就可以顺利解决了。如果依旧报npm的错,那么请调整npm的版本。修改npm@4.0.0后面的版本号即可。

验证安装是否成功

执行命令:

mindinsight start

提示如下,说明安装成功:

Web address: http://127.0.0.1:8080
service start state: success

MindConverter初体验。

MindConverter是一款用于将PyTorch、TensorFlow脚本转换到MindSpore脚本的工具。为了方便我的代码迁移,想使用提供的工具。由于我的代码是Pytorch,就说一下我的体验和感受。

转换pytorch代码

命令如下,一次只能转换一个py文件。

mindconverter --in_file ./my_model_train.py \
              --output ./output \
              --report ./output/report

只需要修改"in_file"为你要转换的代码,output文件夹下就会出现转换后的代码。使用起来非常方便。

report用来说明转换中那些是成功那些还不对的。截取我的转换结果如下:

line 17:0: [UnConvert] 'from torch.utils.data import DataLoader' didn't convert. Please manual convert the code, along with the code associated with it.
line 19:0: [UnConvert] 'from torch.autograd import Variable' didn't convert. Please manual convert the code, along with the code associated with it.
line 20:0: [Convert] 'import torch.nn.functional as F' is converted to 'import mindspore.ops.operations as P'.
line 21:0: [UnConvert] 'import torch.autograd as autograd' didn't convert. Please manual convert the code, along with the code associated with it.
line 22:0: [UnConvert] 'import torch.utils.data as data' didn't convert. Please manual convert the code, along with the code associated with it.

其中UnConvert就是没有转换成功的。这个工具看起来非常方便,但是由于逻辑的不同很多操作还是需要手动去修改。

  • 不推荐使用这个工具作为整体的转换
  • 推荐使用这个工具转换Net部分(如几层的卷积层),对于这部分转换还是很准确的。
  • 转换PyTorch算子的工具

原文先发布在我个人微信公众号,这里属于转载过来。原文

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值