使用MindStudio的X2MindSpore工具进行训练脚本转换

一、参考资料

X2MindSpore

X2MindSpore功能介绍

二、相关介绍

X2MindSpore脚本转换工具可将PyTorch脚本和TensorFlow 2.x脚本转换成MindSpore代码。

2.1 重要说明

当前仅支持PyTorch和TensorFlow 2.x训练脚本转换。

三、关键步骤

本文以 TensorFlow版本的FCN模型为例。

3.1 下载源码

下载链接:FCN

博主的代码:https://gitee.com/lljyoyo1995/fcn

3.2 准备数据集

下载VOC数据集

$ wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
$ wget http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar
$ wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar

解压并整理数据集

VOC           # path:  /home/yang/dataset/VOC
├── test
|    └──VOCdevkit
|        └──VOC2007 (from VOCtest_06-Nov-2007.tar)
└── train
     └──VOCdevkit
         └──VOC2007 (from VOCtrainval_06-Nov-2007.tar)
         └──VOC2012 (from VOCtrainval_11-May-2012.tar)

3.3 生成txt标注文件

python parser_voc.py --voc_path /PATH/TO/VOC

3.4 修改源码

parser_voc.py 中使用的 scipy.misc.imread方法为scipy 1.2.0以前的旧版本API,mindspore最低兼容scipy 1.5.2,因此请使用scipy的官方弃用警告中推荐的 imageio.imread

解决办法:
1. 安装imageio
pip install imageio

2. 修改parser_voc.py源码
label_image = np.array(misc.imread(label_path))
改为
label_image = np.array(imageio.imread(label_path))

3.5 本地跑通代码

如果在TensorFlow 1.X版本下运行,请开启紧急执行

import tensorflow as tf
tf.enable_eager_execution()

拉起训练

python train.py

在这里插入图片描述

推理测试

python test.py

在这里插入图片描述

3.6 Framework Trans

前提条件:跑通代码之后,才能进行 Framework Trans 操作。

安装依赖包

#pandas版本号需大于或等于1.2.4
pip3 install pandas
pip3 install libcst

执行转换

X2MindSpore操作指南

cd Ascend-cann-toolkit安装目录/ascend-toolkit/{
    
    version}/{
    
    arch}-linux/toolkit/tools/x2mindspore/

./run_x2mindspore.sh -i 原始脚本路径 -o 脚本转换结果输出路径 -f 原始脚本的框架

四、TensorFlow GPU2Ascend

4.1 设置Remote CANN

在这里插入图片描述

重启MindStudio激活CANN
在这里插入图片描述

设置成功
在这里插入图片描述
在这里插入图片描述

4.2 设置TensorFlow GPU2Ascend

在这里插入图片描述

# CANN Machine
[email protected]:32542

# Command File
/home/ma-user/Ascend/tfplugin/5.0.3/python/site-packages/npu_bridge/convert_tf2npu/main.py

# Input Paht
\PATH\TO\TensorFlow2.0-Examples\5-Image_Segmentation\FCN

# Output Path
\PATH\TO\TensorFlow2.0-Examples\5-Image_Segmentation

在这里插入图片描述

确定yes
在这里插入图片描述

五、FAQ

Q:设置Remote CANN失败

在这里插入图片描述

错误原因:
远程服务器中未安装ascend-toolkit开发工具

解决方法一:
安装ascend-toolkit开发工具


解决方法二:
找一台已安装ascend-toolkit的服务器

Q:TensorFlow版本不一致导致训练错误

tensorflow.python.framework.errors_impl.InvalidArgumentError: Can not squeeze dim[2], expected a dimension of 1, got 224
	 [[{
    
    {
    
    node metrics/acc/Squeeze}}]]
result = visual_result(x[0], pred_label[0].numpy())
AttributeError: 'Tensor' object has no attribute 'numpy'
错误原因:
源代码支持TF2.X,但博主的环境是TF1.X

解决方法一:
在TF2.X环境中运行

解决方法二:
在TF1.X环境中运行,需要开启紧急执行
import tensorflow as tf
tf.enable_eager_execution()

Q:h5py版本不一致导致推理错误

报错:original_keras_version = f.attrs[‘keras_version‘].decode(‘utf8‘)及问题解决

original_keras_version = f.attrs['keras_version'].decode('utf8')
AttributeError: 'str' object has no attribute 'decode'
错误原因:
在安装tensorflow时,默认安装h5py为3.1.0,而报错是因为你安装的TF不支持过高版本的h5py。

解决办法:
卸载h5py3.1.0版本,安装h5py2.10.0版本
pip install h5py==2.10.0

Q:训练卡住

在这里插入图片描述

错误原因:
用parser_voc.py生成的train_image.txt中的图片路径错误

解决办法:
\反斜杠
改成
/斜杠

在这里插入图片描述

Q:缺少libcst

在这里插入图片描述

错误原因:
缺少libcst依赖

解决办法:
安装libcst
pip3 install libcst

Q:缺少X2MindSpore工具

在这里插入图片描述

错误原因:
服务器中缺少X2MindSpore工具

解决办法:
安装X2MindSpore工具

猜你喜欢

转载自blog.csdn.net/m0_37605642/article/details/125862024
今日推荐