【MindSpore第五期集训营】第一节课MindIR作业

转载地址:https://bbs.huaweicloud.com/forum/thread-105791-1-1.html

作者:yexi

  • MindIR概念介绍

        MindIR的全称为MindSpore IR,是MindSpore的一种基于图表示的函数式IR,定义了可扩展的图结构以及算子的IR表示,消除了不同后端的模型差异,一般用于跨硬件平台执行推理任务。(来自课程PPT)

  • 作业1

   1. 任务

        导出LeNet网络的MindIR格式模型。

   2. 过程

        根据官网https://www.mindspore.cn 中的安装教程在CPU环境的Windows10系统上安装MindSpore。

        根据https://www.mindspore.cn/tutorial/training/zh-CN/r1.1/quick_start/quick_start.html  中的教程“实现一个图片分类应用”,使用LeNet模型对MNIST数据集进行分类,得到Checkpoint格式模型checkpoint_lenet-1_1875.ckpt。

        根据https://www.mindspore.cn/tutorial/training/zh-CN/r1.1/use/save_model.html#mindir 中的“导出MindIR格式文件”内容,将所得的checkpoint_lenet-1_1875.ckpt模型文件转成MindIR格式,代码如下所示:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

import numpy as np

import mindspore.nn as nn

from mindspore.common.initializer import Normal

from mindspore import Tensor, export, load_checkpoint, load_param_into_net

class LeNet5(nn.Cell):

    """Lenet network structure."""

    # define the operator required

    def __init__(self, num_class=10, num_channel=1):

        super(LeNet5, self).__init__()

        self.conv1 = nn.Conv2d(num_channel, 65, pad_mode='valid')

        self.conv2 = nn.Conv2d(6165, pad_mode='valid')

        self.fc1 = nn.Dense(16 * 5 * 5120, weight_init=Normal(0.02))

        self.fc2 = nn.Dense(12084, weight_init=Normal(0.02))

        self.fc3 = nn.Dense(84, num_class, weight_init=Normal(0.02))

        self.relu = nn.ReLU()

        self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)

        self.flatten = nn.Flatten()

    # use the preceding operators to construct networks

    def construct(self, x):

        = self.max_pool2d(self.relu(self.conv1(x)))

        = self.max_pool2d(self.relu(self.conv2(x)))

        = self.flatten(x)

        = self.relu(self.fc1(x))

        = self.relu(self.fc2(x))

        = self.fc3(x)

        return x

lenet_mnist_model = LeNet5()

# load the parameter into net

load_checkpoint("checkpoint_lenet-1_1875.ckpt", net=lenet_mnist_model)

input = np.random.uniform(0.01.0, size=[3213232]).astype(np.float32)

export(lenet_mnist_model, Tensor(input), file_name='lenet_mnist_MindIR_model-1_1875', file_format='MINDIR')

  • 作业2

   1. 任务

        训练一个ResNet50网络。使用训练好的checkpoint文件,导出MindIR格式模型。

   2. 过程

        继续使用https://www.mindspore.cn/tutorial/training/zh-CN/r1.1/quick_start/quick_start.html 中的代码模板,其中定义模型结构的代码需要改成ResNet50网络模型(可从https://gitee.com/mindspore/mindspore/blob/master/model_zoo/official/cv/resnet/src/resnet.py 中获取 ),使用的数据集改成hymenoptera数据集(下载地址为https://download.pytorch.org/tutorial/hymenoptera_data.zip ,需要删除训练集中ants文件夹下的一张.gif格式的图片)。训练修改后的程序得到Checkpoint格式模型。

        同样根据https://www.mindspore.cn/tutorial/training/zh-CN/r1.1/use/save_model.html#mindir 中的“导出MindIR格式文件”内容,将所得的Checkpoint格式模型文件转成MindIR格式,代码如下所示:

1

2

3

4

5

6

7

8

9

10

import numpy as np

from mindspore import Tensor, export, load_checkpoint, load_param_into_net

import resnet_mindspore_model as resnet_model

resnet_hymenoptera_model = resnet_model.resnet50(2)

# load the parameter into net

load_checkpoint("checkpoint_resnet50-5_15.ckpt", net=resnet_hymenoptera_model)

input = np.random.uniform(0.01.0, size=[163224224]).astype(np.float32)  # 注意输入尺寸

export(resnet_hymenoptera_model, Tensor(input), file_name='resnet_hymenoptera_MindIR_model-5_15', file_format='MINDIR')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值