[{"data":1,"prerenderedAt":284},["ShallowReactive",2],{"content-query-Zz9xThm5L5":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":278,"_id":279,"_source":280,"_file":281,"_stem":282,"_extension":283},"/technology-blogs/zh/605","zh",false,"","开发者分享 | 5分钟在线体验MindIR，快速实现端云互通","MindIR可以帮助我们实现一次训练多处部署，实现端云互通","2021-06-04","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/04/a8b16497aaea40b590147922beb43177.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":273},"root",[17,25,34,48,60,65,71,85,93,98,104,115,120,125,132,137,147,152,159,164,170,180,185,192,197,202,210,215,223,230,235,240,251,256,261,266],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"开发者分享-5分钟在线体验mindir快速实现端云互通",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29],{"type":18,"tag":30,"props":31,"children":33},"img",{"alt":7,"src":32},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/04/f7805375389d42aa82d1c95f46061314.gif",[],{"type":18,"tag":26,"props":35,"children":36},{},[37,39],{"type":24,"value":38},"本文来源于：",{"type":18,"tag":40,"props":41,"children":45},"a",{"href":42,"rel":43},"https://bbs.huaweicloud.com/forum/forum-1076-1.html",[44],"nofollow",[46],{"type":24,"value":47},"MindSpore论坛",{"type":18,"tag":26,"props":49,"children":50},{},[51,53],{"type":24,"value":52},"作者：",{"type":18,"tag":40,"props":54,"children":57},{"href":55,"rel":56},"https://bbs.huaweicloud.com/forum/home.php?mod=space&uid=204796&do=thread",[44],[58],{"type":24,"value":59},"胡琦",{"type":18,"tag":26,"props":61,"children":62},{},[63],{"type":24,"value":64},"众所周知，人工智能能有今天的发展离不开数据、算力和算法。作为普通的Copy攻城狮，手头没有良好的硬件支撑AI的学习，焦头烂额，还好有她--ModelArts 。ModelArts是华为云的一站式AI开发平台，其中有个我的笔记本功能，是即开即用、用于机器学习的在线集成开发环境，可以轻松的构建、训练、调试、部署机器学习算法与模型。关键是免费！免费！免费！",{"type":18,"tag":19,"props":66,"children":68},{"id":67},"关于mindir",[69],{"type":24,"value":70},"关于MindIR",{"type":18,"tag":26,"props":72,"children":73},{},[74,76,83],{"type":24,"value":75},"MindIR是MindSpore提供的中间表达形式，可以帮助我们实现一次训练多处部署，实现端云互通。比如我们可以在Ascend AI处理器、GPU、CPU等不同平台上训练并导出MindIR文件，然后快速部署到手机端或者用MindSpore Serving服务等环境上，体验到MindSpore的全场景能力。MindIR的具体实现细节请阅读",{"type":18,"tag":40,"props":77,"children":80},{"href":78,"rel":79},"http://mp.weixin.qq.com/s?__biz=MzAxMDA1MDM0NQ==&mid=2247488022&idx=1&sn=3bbf8eb5898254ef5f44a98676709233&chksm=9b571c50ac2095468e28fa82bba7883ea447ffefca447cf406d0399fa32bb93b43ecc0b1c5f3&scene=21#wechat_redirect",[44],[81],{"type":24,"value":82},"MindSpore技术专栏 | AI框架中图层IR的分析",{"type":24,"value":84},"。因为，Copy攻城狮只关心如何应用：",{"type":18,"tag":86,"props":87,"children":88},"h2",{"id":7},[89],{"type":18,"tag":30,"props":90,"children":92},{"alt":7,"src":91},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/04/10cf4ba72d384ea4b1a6971b6f9b8455.jpg",[],{"type":18,"tag":26,"props":94,"children":95},{},[96],{"type":24,"value":97},"接下来通过两个具体的在线案例了解一下 MindIR 格式的模型是如何生成的。",{"type":18,"tag":19,"props":99,"children":101},{"id":100},"导出lenet网络的mindir格式模型",[102],{"type":24,"value":103},"导出LeNet网络的MindIR格式模型",{"type":18,"tag":26,"props":105,"children":106},{},[107,109],{"type":24,"value":108},"官方文档：",{"type":18,"tag":40,"props":110,"children":113},{"href":111,"rel":112},"https://www.mindspore.cn/tutorial/training/zh-CN/r1.1/use/save_model.html",[44],[114],{"type":24,"value":111},{"type":18,"tag":26,"props":116,"children":117},{},[118],{"type":24,"value":119},"这是一个可以在 CPU 环境运行的案例，使用的是 LeNet 网络 和 MNIST 数据集。具体的实现本文不再赘述，总之这个案例详细介绍了 MindSpore 中数据处理、网络定义、模型训练、模型保存、模型验证等相关知识点。本文关注点在导出MindIR格式文件。",{"type":18,"tag":26,"props":121,"children":122},{},[123],{"type":24,"value":124},"点击上边或者官方文档的 Run in ModelArts 就可跳转到华为云一站式AI开发平台，我们进入的其实是 ModelArts 的 我的笔记本功能--一个基于 JupyterLab 构建的在线集成开发环境，还可以快速切换不同环境平台。当前案例使用的是 GPU 环境，MindSpore 版本为 1.0.1 。根据文档并执行代码，我们很快就能实现一个 LeNet 网络并得到训练之后的模型。",{"type":18,"tag":26,"props":126,"children":127},{},[128],{"type":18,"tag":30,"props":129,"children":131},{"alt":7,"src":130},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/04/7c70398d29f0453c81a2b9027f6bf54e.jpg",[],{"type":18,"tag":26,"props":133,"children":134},{},[135],{"type":24,"value":136},"目前导出 MINDIR 格式模型文件基于前置步骤生成的 CheckPoint 文件。官方文档建议使用.mindir作为MINDIR格式文件的后缀名。导出 MINDIR 格式文件的代码如下：",{"type":18,"tag":138,"props":139,"children":141},"pre",{"code":140},"from mindspore.train.serialization import export, load_checkpoint, load_param_into_net\nfrom mindspore import Tensor\nimport numpy as np\n\nlenet = LeNet5()\n# 返回模型的参数字典\nparam_dict = load_checkpoint(\"./models/ckpt/mindspore_save_model/lenet-1_1875.ckpt\")\n# 加载参数到网络\nload_param_into_net(lenet, param_dict)\ninput = np.random.uniform(0.0, 1.0, size=[32, 1, 32, 32]).astype(np.float32)\n# 以指定的名称和格式导出文件\nexport(lenet, Tensor(input), file_name='lenet-1_1875.mindir', file_format='MINDIR',)\n",[142],{"type":18,"tag":143,"props":144,"children":145},"code",{"__ignoreMap":7},[146],{"type":24,"value":140},{"type":18,"tag":26,"props":148,"children":149},{},[150],{"type":24,"value":151},"导出完成之后，在当前目录下会生成一个MINDIR格式文件，如下图中名为lenet-1_1875.mindir的文件。",{"type":18,"tag":26,"props":153,"children":154},{},[155],{"type":18,"tag":30,"props":156,"children":158},{"alt":7,"src":157},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/04/213980fbe0c34089afcff24f5bc86fbf.jpg",[],{"type":18,"tag":26,"props":160,"children":161},{},[162],{"type":24,"value":163},"当然如果您想将 LeNet 模型导出为不同格式的文件，建议您进行封装，具体实践请参考：mindspore/model_zoo/official/cv/lenet/export.py",{"type":18,"tag":19,"props":165,"children":167},{"id":166},"训练resnet50网络导出mindir格式模型",[168],{"type":24,"value":169},"训练ResNet50网络导出MindIR格式模型",{"type":18,"tag":26,"props":171,"children":172},{},[173,174],{"type":24,"value":108},{"type":18,"tag":40,"props":175,"children":178},{"href":176,"rel":177},"https://www.mindspore.cn/tutorial/training/zh-CN/r1.1/advanced_use/cv_resnet50.html",[44],[179],{"type":24,"value":176},{"type":18,"tag":26,"props":181,"children":182},{},[183],{"type":24,"value":184},"有了上一次的在线体验，在进行训练ResNet50网络导出MindIR格式模型的时候，可谓“轻车熟路”，同样的点击Run in ModelArts开始我们的云端AI开发之旅。这次我们使用的是ResNet-50网络，CIFAR-10数据集，依旧是 MIndSpore 1.0.1 版本，不过是 GPU 环境的。",{"type":18,"tag":26,"props":186,"children":187},{},[188],{"type":18,"tag":30,"props":189,"children":191},{"alt":7,"src":190},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/04/13e8a98c4808499f836bdd36aacd9c37.jpg",[],{"type":18,"tag":26,"props":193,"children":194},{},[195],{"type":24,"value":196},"目前, ModelArts 中 我的笔记本要想使用 GPU 版本的 MIndSpore，好像暂时只有这个入口。",{"type":18,"tag":26,"props":198,"children":199},{},[200],{"type":24,"value":201},"当然，这个案例中并没有实现将模型导出为 MindIR 格式，需要我们手动添加，唯一值得注意的是 MindSpore 1.0.1 和 MIndSpore 1.1.0 中 导出的方法export放在不同的命名空间下，1.1.0版本的引入方法是：",{"type":18,"tag":138,"props":203,"children":205},{"code":204},"from mindspore import Tensor, export, load_checkpoint, load_param_into_net\n",[206],{"type":18,"tag":143,"props":207,"children":208},{"__ignoreMap":7},[209],{"type":24,"value":204},{"type":18,"tag":26,"props":211,"children":212},{},[213],{"type":24,"value":214},"因此，这里导出ResNet50网络 MindIR 格式的代码为：",{"type":18,"tag":138,"props":216,"children":218},{"code":217},"from resnet import resnet50\n\nfrom mindspore.train.serialization import export, load_checkpoint, load_param_into_net\nfrom mindspore import Tensor\nimport numpy as np\nresnet = resnet50(batch_size=32, num_classes=10)\n# return a parameter dict for model\nparam_dict = load_checkpoint(\"./models/ckpt/mindspore_vision_application/train_resnet_cifar10-10_1562.ckpt\")\n# load the parameter into net\nload_param_into_net(resnet, param_dict)\ninput = np.random.uniform(0.0, 1.0, size=[32, 3, 224, 224]).astype(np.float32)\nexport(resnet, Tensor(input), file_name='resnet_cifar10-10_1562.mindir', file_forma\n",[219],{"type":18,"tag":143,"props":220,"children":221},{"__ignoreMap":7},[222],{"type":24,"value":217},{"type":18,"tag":26,"props":224,"children":225},{},[226],{"type":18,"tag":30,"props":227,"children":229},{"alt":7,"src":228},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/04/0c017b2028c64251b0db20f968437bc5.jpg",[],{"type":18,"tag":26,"props":231,"children":232},{},[233],{"type":24,"value":234},"不过这两个在线案例导出之后的 MindIR 格式，我还没来得及验证是否能正常使用。如果您知道快速验证的方法，请您分享与我，感谢大家多多指教！",{"type":18,"tag":26,"props":236,"children":237},{},[238],{"type":24,"value":239},"MindSpore官方资料",{"type":18,"tag":26,"props":241,"children":242},{},[243,245],{"type":24,"value":244},"GitHub : ",{"type":18,"tag":40,"props":246,"children":249},{"href":247,"rel":248},"https://github.com/mindspore-ai/mindspore",[44],[250],{"type":24,"value":247},{"type":18,"tag":26,"props":252,"children":253},{},[254],{"type":24,"value":255},"Gitee:[https : //gitee.com/mindspore/mindspore](https : //gitee.com/mindspore/mindspore)",{"type":18,"tag":26,"props":257,"children":258},{},[259],{"type":24,"value":260},"官方QQ群 : 871543426",{"type":18,"tag":26,"props":262,"children":263},{},[264],{"type":24,"value":265},"长按下方二维码加入MindSpore项目↓",{"type":18,"tag":26,"props":267,"children":268},{},[269],{"type":18,"tag":30,"props":270,"children":272},{"alt":7,"src":271},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/04/f37b67acf6f2414bb7ce0aaf7d6d5fba.jpg",[],{"title":7,"searchDepth":274,"depth":274,"links":275},4,[276],{"id":7,"depth":277,"text":7},2,"markdown","content:technology-blogs:zh:605.md","content","technology-blogs/zh/605.md","technology-blogs/zh/605","md",1776506138616]