当前位置:网站首页>Ascend 910实现Tensorflow1.15实现LeNet网络的minist手写数字识别
Ascend 910实现Tensorflow1.15实现LeNet网络的minist手写数字识别
2022-07-07 12:35:00 【华为云】
一、环境及准备工作
CPU/GPU复现使用华为云ModelArts-CodeLab平台
Ascend复现使用华为云ModelArts-开发环境-Notebook
原始Lenet代码链接:https://gitee.com/lai-pengfei/LeNet
二、在CPU/GPU中运行原始代码
第一步:打开CodeLab
注:如果需要切换GPU资源,可以点
资源选择GPU资源,可以使用1小时,1小时候需要手动续时
点击页面中的Terminal进入终端界面:
终端界面:
第二步:进入到work目录并git clone 相关代码
git clone https://gitee.com/lai-pengfei/LeNet
可以看到左面有git下来的代码了
第三步:切换TensorFlow运行环境
source activate /home/ma-user/anaconda3/envs/TensorFlow-1.13.1/
这里是1.13的,但是问题不大,大差不差,也可以跑和1.15差不多
第四步:进入文件夹并执行原始代码
cd cd LeNet/python Train.py
运行中:
运行结果:
三、模型移植
环境使用华为云-开发环境Notebook
创建环境
镜像选择勾选的
规格等设置如下图:
环境及代码下载参考CPU/GPU
切换到Ascend下的TensorFlow 1.15运行环境
source activate /home/ma-user/anaconda3/envs/TensorFlow-1.15.0/
代码修改
根据文档修改原始代码中的Train.py
文档地址:https://www.hiascend.com/document/detail/zh/CANNCommunityEdition/51RC2alpha007/moddevg/tfmigr/atlasmprtg_13_0011.html
添加引入包的代码:
from npu_bridge.npu_init import *
修改创建session并初始化资源相关代码
这一步主要在sess.run(tf.initialize_all_variables())前添加以下几行代码
config = tf.ConfigProto()custom_op = config.graph_options.rewrite_options.custom_optimizers.add()custom_op.name = "NpuOptimizer"config.graph_options.rewrite_options.remapping = RewriterConfig.OFF # 必须显式关闭config.graph_options.rewrite_options.memory_optimization = RewriterConfig.OFF # 必须显式关闭sess = tf.Session(config=config)
这个Demo还需要改一下第一行相关库代码
原始代码:
import tensorflow.examples.tutorials.mnist.input_data as input_data
修改为:
from tensorflow.examples.tutorials.mnist import input_data
运行代码
python Train.py
看到W tf_adapt差不多字样就说明调用到了NPU资源
运行过程:
可以开启另一个Terminal查看是否真的使用了Ascend,在新的Terminal中使用如下命令:
npu-smi info
运行结果:
总结
到这里整个简单的TensorFlow移植Ascend平台运行的代码修改就完成了,其实整个过程算是比较简单的,会者不难,不会可能会觉得很难。模型迁移的话主要难点在可能存在有些算子不支持以及精度性能优化部分。
边栏推荐
- Horizontal of libsgm_ path_ Interpretation of aggregation program
- MySQL "invalid use of null value" solution
- 2022PAGC 金帆奖 | 融云荣膺「年度杰出产品技术服务商」
- Parameter keywords final, flags, internal, mapping keywords internal
- 交换机和路由器的异同
- c#通过frame 和 page 切换页面
- UML state diagram
- Equipment failure prediction machine failure early warning mechanical equipment vibration monitoring machine failure early warning CNC vibration wireless monitoring equipment abnormal early warning
- CVPR2022 | 医学图像分析中基于频率注入的后门攻击
- wpf dataGrid 实现单行某个数据变化 ui 界面随之响应
猜你喜欢
Transferring files between VMware and host
OAuth 2.0 + JWT protect API security
GVIM [III] [u vimrc configuration]
一个简单LEGv8处理器的Verilog实现【四】【单周期实现基础知识及模块设计讲解】
Multi merchant mall system function disassembly lecture 01 - Product Architecture
Hands on Teaching: XML modeling
[Reading stereo matching papers] [III] ints
【立体匹配论文阅读】【三】INTS
VSCode 配置使用 PyLint 语法检查器
gvim【三】【_vimrc配置】
随机推荐
杭电oj2054 A == B ? ???
Hangdian oj2092 integer solution
请问,在使用flink sql sink数据到kafka的时候出现执行成功,但是kafka里面没有数
2022PAGC 金帆奖 | 融云荣膺「年度杰出产品技术服务商」
How does MySQL control the number of replace?
OAuth 2.0 + JWT protect API security
docker部署oracle
Reverse non return to zero code, Manchester code and differential Manchester code of common digital signal coding
手把手教会:XML建模
The longest ascending subsequence model acwing 482 Chorus formation
请问,我kafka 3个分区,flinksql 任务中 写了 join操作,,我怎么单独给join
Similarities and differences between switches and routers
Leetcode - Sword finger offer 05 Replace spaces
常用數字信號編碼之反向不歸零碼碼、曼徹斯特編碼、差分曼徹斯特編碼
First choice for stock account opening, lowest Commission for stock trading account opening, is online account opening safe
【网络安全】sql注入语法汇总
CSMA/CD 载波监听多点接入/碰撞检测协议
搜索框效果的实现【每日一题】
SAKT方法部分介绍
Assign a dynamic value to the background color of DataGrid through ivalueconverter