[{"data":1,"prerenderedAt":574},["ShallowReactive",2],{"content-query-kIk04oqz5Y":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":568,"_id":569,"_source":570,"_file":571,"_stem":572,"_extension":573},"/technology-blogs/zh/3042","zh",false,"","基于MindSpore框架的深度学习模型在CV方向的应用--（1）MindSpore API的基础和进阶(一)","1 实验介绍","2024-03-25","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/03/29/7669c4ae1da44f428b21fc8236d7630b.png","technology-blogs","实践",{"type":15,"children":16,"toc":556},"root",[17,25,30,37,43,49,54,59,64,70,75,81,86,92,98,103,108,113,118,128,133,138,143,148,156,161,169,174,182,187,195,200,208,213,221,226,231,236,241,249,254,259,267,273,278,283,291,296,301,306,314,320,325,330,335,340,345,353,358,363,368,376,381,386,391,399,405,410,415,420,428,433,438,443,448,456,461,466,471,476,481,486,494,499,504,509,514,522,527,532,537,545,551],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"基于mindspore框架的深度学习模型在cv方向的应用-1mindspore-api的基础和进阶一",[23],{"type":24,"value":8},"text",{"type":18,"tag":19,"props":26,"children":28},{"id":27},"_1-实验介绍",[29],{"type":24,"value":9},{"type":18,"tag":31,"props":32,"children":34},"h2",{"id":33},"_11-关于本实验",[35],{"type":24,"value":36},"1.1 关于本实验",{"type":18,"tag":38,"props":39,"children":40},"p",{},[41],{"type":24,"value":42},"本实验通过介绍MindSpore的数据结构与数据类型，MindSpore搭建神经网络用到的基础模块，比如数据集加载，神经网络搭建，模型训练与评估等，让学员熟悉MindSpore的基础用法，掌握MindSpore开发的简单流程。",{"type":18,"tag":31,"props":44,"children":46},{"id":45},"_12-实验目的",[47],{"type":24,"value":48},"1.2 实验目的",{"type":18,"tag":38,"props":50,"children":51},{},[52],{"type":24,"value":53},"理解MindSpore开发基本流程。",{"type":18,"tag":38,"props":55,"children":56},{},[57],{"type":24,"value":58},"理解MindSpore基础模块的功能。",{"type":18,"tag":38,"props":60,"children":61},{},[62],{"type":24,"value":63},"掌握MindSpore的简单操作。",{"type":18,"tag":31,"props":65,"children":67},{"id":66},"_13-背景知识",[68],{"type":24,"value":69},"1.3 背景知识",{"type":18,"tag":38,"props":71,"children":72},{},[73],{"type":24,"value":74},"神经网络知识，感知机，多层感知机，前向传播，反向传播，激活函数，损失函数，优化器，评估方法。",{"type":18,"tag":31,"props":76,"children":78},{"id":77},"_14-实验设计",[79],{"type":24,"value":80},"1.4 实验设计",{"type":18,"tag":38,"props":82,"children":83},{},[84],{"type":24,"value":85},"1.张量和数据类型 2.数据集加载 3.全连接网络搭建 4.模型训练 5.模型评估",{"type":18,"tag":19,"props":87,"children":89},{"id":88},"_2-实验过程",[90],{"type":24,"value":91},"2 实验过程",{"type":18,"tag":31,"props":93,"children":95},{"id":94},"_21-张量和数据类型",[96],{"type":24,"value":97},"2.1 张量和数据类型",{"type":18,"tag":38,"props":99,"children":100},{},[101],{"type":24,"value":102},"张量（Tensor）是MindSpore网络运算中的基本数据结构。张量中的数据类型可参考dtype。 不同维度的张量分别表示不同的数据，0维张量表示标量，1维张量表示向量，2维张量表示矩阵，3维张量可以表示彩色图像的RGB三通道等等。",{"type":18,"tag":38,"props":104,"children":105},{},[106],{"type":24,"value":107},"MindSpore张量支持不同的数据类型，包含int8、int16、int32、int64、uint8、uint16、uint32、uint64、float16、float32、float64、bool_，与NumPy的数据类型一一对应。 在MindSpore的运算处理流程中，Python中的int数会被转换为定义的int64类型，float数会被转换为定义的float32类型。",{"type":18,"tag":38,"props":109,"children":110},{},[111],{"type":24,"value":112},"步骤 1 指定MindSpore数据类型",{"type":18,"tag":38,"props":114,"children":115},{},[116],{"type":24,"value":117},"导入MindSpore，设置Jupyter notebook的cell同时输出多行。",{"type":18,"tag":119,"props":120,"children":122},"pre",{"code":121},"#导入MindSpore\nimport mindspore\nfrom mindspore import dtype \nfrom mindspore import Tensor\n#cell同时输出多行\nfrom IPython.core.interactiveshell import InteractiveShell\nInteractiveShell.ast_node_interactivity = \"all\"\n\n#指定数据类型\na = 1\ntype(a)\nb = Tensor(a, dtype.float64) \nb.dtype \n",[123],{"type":18,"tag":124,"props":125,"children":126},"code",{"__ignoreMap":7},[127],{"type":24,"value":121},{"type":18,"tag":38,"props":129,"children":130},{},[131],{"type":24,"value":132},"步骤 2 张量构造",{"type":18,"tag":38,"props":134,"children":135},{},[136],{"type":24,"value":137},"构造张量时，支持传入Tensor、float、int、bool、tuple、list和NumPy.array类型，其中tuple和list里只能存放float、int、bool类型数据。",{"type":18,"tag":38,"props":139,"children":140},{},[141],{"type":24,"value":142},"Tensor初始化时，可指定dtype。如果没有指定dtype，初始值int、float、bool分别生成数据类型为mindspore.int32、mindspore.float32、mindspore.bool_的0维Tensor， 初始值tuple和list生成的1维Tensor数据类型与tuple和list里存放的数据类型相对应，如果包含多种不同类型的数据，则按照优先级：bool \u003C int \u003C float，选择相对优先级最高类型所对应的mindspore数据类型。 如果初始值是Tensor，则生成的Tensor数据类型与其一致；如果初始值是NumPy.array，则生成的Tensor数据类型与之对应。",{"type":18,"tag":38,"props":144,"children":145},{},[146],{"type":24,"value":147},"用数组创建张量",{"type":18,"tag":119,"props":149,"children":151},{"code":150},"import numpy as np\nfrom mindspore import Tensor\n\n# 用数组创建张量\nx = Tensor(np.array([[1, 2], [3, 4]]), dtype.int32)\nx\n",[152],{"type":18,"tag":124,"props":153,"children":154},{"__ignoreMap":7},[155],{"type":24,"value":150},{"type":18,"tag":38,"props":157,"children":158},{},[159],{"type":24,"value":160},"用数值创建张量",{"type":18,"tag":119,"props":162,"children":164},{"code":163},"#用数值创建张量\ny = Tensor(1.0, dtype.int32)\nz = Tensor(2, dtype.int32)\ny\nz\n",[165],{"type":18,"tag":124,"props":166,"children":167},{"__ignoreMap":7},[168],{"type":24,"value":163},{"type":18,"tag":38,"props":170,"children":171},{},[172],{"type":24,"value":173},"用Bool创建张量",{"type":18,"tag":119,"props":175,"children":177},{"code":176},"#用Bool创建张量\nm = Tensor(True, dtype.bool_)\nm\n",[178],{"type":18,"tag":124,"props":179,"children":180},{"__ignoreMap":7},[181],{"type":24,"value":176},{"type":18,"tag":38,"props":183,"children":184},{},[185],{"type":24,"value":186},"用tuple创建张量",{"type":18,"tag":119,"props":188,"children":190},{"code":189},"#用tuple创建张量\nn = Tensor((1, 2, 3), dtype.int16)\nn\n",[191],{"type":18,"tag":124,"props":192,"children":193},{"__ignoreMap":7},[194],{"type":24,"value":189},{"type":18,"tag":38,"props":196,"children":197},{},[198],{"type":24,"value":199},"用list创建张量",{"type":18,"tag":119,"props":201,"children":203},{"code":202},"#用list创建张量\np = Tensor([4.0, 5.0, 6.0], dtype.float64)\np\n",[204],{"type":18,"tag":124,"props":205,"children":206},{"__ignoreMap":7},[207],{"type":24,"value":202},{"type":18,"tag":38,"props":209,"children":210},{},[211],{"type":24,"value":212},"用常量创建张量",{"type":18,"tag":119,"props":214,"children":216},{"code":215},"#用常量创建张量\nq = Tensor(1, dtype.float64)\nq\n",[217],{"type":18,"tag":124,"props":218,"children":219},{"__ignoreMap":7},[220],{"type":24,"value":215},{"type":18,"tag":38,"props":222,"children":223},{},[224],{"type":24,"value":225},"步骤 3 张量的属性¶",{"type":18,"tag":38,"props":227,"children":228},{},[229],{"type":24,"value":230},"张量的属性包括形状（shape）和数据类型（dtype）。",{"type":18,"tag":38,"props":232,"children":233},{},[234],{"type":24,"value":235},"形状：Tensor的shape，是一个tuple。",{"type":18,"tag":38,"props":237,"children":238},{},[239],{"type":24,"value":240},"数据类型：Tensor的dtype，是MindSpore的一个数据类型。",{"type":18,"tag":119,"props":242,"children":244},{"code":243},"x = Tensor(np.array([[1, 2], [3, 4]]), dtype.int32)\nx_shape = x.shape  # 形状\nx_dtype = x.dtype  # 数据类型\n\nx_shape\nx_dtype\n\nx = Tensor(np.array([[1, 2], [3, 4]]), dtype.int32)\n\nx.shape # 形状\nx.dtype # 数据类型\nx.ndim  # 维度\nx.size  # 大小\n",[245],{"type":18,"tag":124,"props":246,"children":247},{"__ignoreMap":7},[248],{"type":24,"value":243},{"type":18,"tag":38,"props":250,"children":251},{},[252],{"type":24,"value":253},"步骤 4 张量的方法",{"type":18,"tag":38,"props":255,"children":256},{},[257],{"type":24,"value":258},"asnumpy()：将Tensor转换为NumPy的array。",{"type":18,"tag":119,"props":260,"children":262},{"code":261},"y = Tensor(np.array([[True, True], [False, False]]), dtype.bool_)\n\n# 将Tensor数据类型转换成NumPy\ny_array = y.asnumpy()\n\ny\ny_array\n",[263],{"type":18,"tag":124,"props":264,"children":265},{"__ignoreMap":7},[266],{"type":24,"value":261},{"type":18,"tag":31,"props":268,"children":270},{"id":269},"_22-数据集加载",[271],{"type":24,"value":272},"2.2 数据集加载",{"type":18,"tag":38,"props":274,"children":275},{},[276],{"type":24,"value":277},"MindSpore.dataset提供API来加载和处理各种常见的数据集，如MNIST, CIFAR-10, CIFAR-100, VOC, ImageNet, CelebA等。",{"type":18,"tag":38,"props":279,"children":280},{},[281],{"type":24,"value":282},"步骤 1 加载MNIST数据集",{"type":18,"tag":119,"props":284,"children":286},{"code":285},"mindspore.dataset.MnistDataset\n\nimport os\nimport mindspore.dataset as ds\nimport matplotlib.pyplot as plt\n\ndataset_dir = \"./data/train\"  # 数据集路径\n\n# 从mnist dataset读取3张图片\nmnist_dataset = ds.MnistDataset(dataset_dir=dataset_dir, num_samples=3)\n\n# 设置图像大小\nplt.figure(figsize=(8,8))\ni = 1\n\n# 打印3张子图\nfor dic in mnist_dataset.create_dict_iterator(output_numpy=True):\n    plt.subplot(3,3,i)\n    plt.imshow(dic['image'][:,:,0])\n    plt.axis('off')\n    i +=1\n\nplt.show()\n",[287],{"type":18,"tag":124,"props":288,"children":289},{"__ignoreMap":7},[290],{"type":24,"value":285},{"type":18,"tag":38,"props":292,"children":293},{},[294],{"type":24,"value":295},"MindSpore还支持加载多种数据存储格式下的数据集，用户可以直接使用mindspore.dataset中对应的类加载磁盘中的数据文件。",{"type":18,"tag":38,"props":297,"children":298},{},[299],{"type":24,"value":300},"步骤 2 加载NumPy数据集",{"type":18,"tag":38,"props":302,"children":303},{},[304],{"type":24,"value":305},"mindspore.dataset.NumpySlicesDataset",{"type":18,"tag":119,"props":307,"children":309},{"code":308},"import mindspore.dataset as ds\n\ndata = ds.NumpySlicesDataset([1, 2, 3], column_names=[\"col_1\"])\nfor x in data.create_dict_iterator():\n    print(x)\n",[310],{"type":18,"tag":124,"props":311,"children":312},{"__ignoreMap":7},[313],{"type":24,"value":308},{"type":18,"tag":31,"props":315,"children":317},{"id":316},"_23-全连接网络搭建",[318],{"type":24,"value":319},"2.3 全连接网络搭建",{"type":18,"tag":38,"props":321,"children":322},{},[323],{"type":24,"value":324},"步骤 1 全连接神经网络",{"type":18,"tag":38,"props":326,"children":327},{},[328],{"type":24,"value":329},"全连接层 mindspore.nn.Dense",{"type":18,"tag":38,"props":331,"children":332},{},[333],{"type":24,"value":334},"in_channels：输入通道",{"type":18,"tag":38,"props":336,"children":337},{},[338],{"type":24,"value":339},"out_channels：输出通道",{"type":18,"tag":38,"props":341,"children":342},{},[343],{"type":24,"value":344},"weight_init：权重初始化，Default 'normal'.",{"type":18,"tag":119,"props":346,"children":348},{"code":347},"import mindspore.nn as nn\nfrom mindspore import Tensor\n\n# 构造输入张量\ninput = Tensor(np.array([[1, 1, 1], [2, 2, 2]]), mindspore.float32)\nprint(input)\n\n# 构造全连接网络，输入通道为3，输出通道为3\nnet = nn.Dense(in_channels=3, out_channels=3, weight_init=1)\noutput = net(input)\nprint(output)\n",[349],{"type":18,"tag":124,"props":350,"children":351},{"__ignoreMap":7},[352],{"type":24,"value":347},{"type":18,"tag":38,"props":354,"children":355},{},[356],{"type":24,"value":357},"步骤 2 激活函数",{"type":18,"tag":38,"props":359,"children":360},{},[361],{"type":24,"value":362},"矫正线性单元激活函数",{"type":18,"tag":38,"props":364,"children":365},{},[366],{"type":24,"value":367},"mindspore.nn.ReLU",{"type":18,"tag":119,"props":369,"children":371},{"code":370},"input_x = Tensor(np.array([-1, 2, -3, 2, -1]), mindspore.float16)\n\nrelu = nn.ReLU()\noutput = relu(input_x)\nprint(output)\n",[372],{"type":18,"tag":124,"props":373,"children":374},{"__ignoreMap":7},[375],{"type":24,"value":370},{"type":18,"tag":38,"props":377,"children":378},{},[379],{"type":24,"value":380},"步骤 3 搭建模型",{"type":18,"tag":38,"props":382,"children":383},{},[384],{"type":24,"value":385},"所有神经网络的基类",{"type":18,"tag":38,"props":387,"children":388},{},[389],{"type":24,"value":390},"mindspore.nn.Cell",{"type":18,"tag":119,"props":392,"children":394},{"code":393},"import mindspore.nn as nn\n\nclass MyCell(nn.Cell):\n    \n    # 定义算子\n    def __init__(self, ):\n        super(MyCell, self).__init__()\n        \n        # 全连接层\n        self.fc = nn.Dense()\n\n        # 激活函数\n        self.relu = nn.ReLU()\n\n    # 建构网络\n    def construct(self, x):\n        x = self.fc(x)\n        x = self.relu(x)\n        return x\n",[395],{"type":18,"tag":124,"props":396,"children":397},{"__ignoreMap":7},[398],{"type":24,"value":393},{"type":18,"tag":31,"props":400,"children":402},{"id":401},"_24-模型训练与评估",[403],{"type":24,"value":404},"2.4 模型训练与评估",{"type":18,"tag":38,"props":406,"children":407},{},[408],{"type":24,"value":409},"步骤 1 损失函数",{"type":18,"tag":38,"props":411,"children":412},{},[413],{"type":24,"value":414},"交叉熵损失函数，用于分类模型。当标签数据不是one-hot编码形式时，需要输入参数sparse为True。",{"type":18,"tag":38,"props":416,"children":417},{},[418],{"type":24,"value":419},"mindspore.nn.SoftmaxCrossEntropyWithLogits",{"type":18,"tag":119,"props":421,"children":423},{"code":422},"import mindspore.nn as nn\n\n# 交叉熵损失函数\nloss = nn.SoftmaxCrossEntropyWithLogits(sparse=True)\n\nnp.random.seed(0)\nlogits = Tensor(np.random.randint(0, 9, [1, 10]), mindspore.float32)\nprint(logits)\n\nlabels_np = np.ones([1,]).astype(np.int32)\nlabels = Tensor(labels_np)\nprint(labels)\n\noutput = loss(logits, labels)\nprint(output)\n",[424],{"type":18,"tag":124,"props":425,"children":426},{"__ignoreMap":7},[427],{"type":24,"value":422},{"type":18,"tag":38,"props":429,"children":430},{},[431],{"type":24,"value":432},"步骤 2 优化器",{"type":18,"tag":38,"props":434,"children":435},{},[436],{"type":24,"value":437},"深度学习优化算法大概常用的有SGD、Adam、Ftrl、lazyadam、Momentum、RMSprop、Lars、Proximal_ada_grad和lamb这几种。",{"type":18,"tag":38,"props":439,"children":440},{},[441],{"type":24,"value":442},"动量优化器",{"type":18,"tag":38,"props":444,"children":445},{},[446],{"type":24,"value":447},"mindspore.nn.Momentum",{"type":18,"tag":119,"props":449,"children":451},{"code":450},"# optim = nn.Momentum(params, learning_rate=0.1, momentum=0.9, weight_decay=0.0) # params是传入的参数\n",[452],{"type":18,"tag":124,"props":453,"children":454},{"__ignoreMap":7},[455],{"type":24,"value":450},{"type":18,"tag":38,"props":457,"children":458},{},[459],{"type":24,"value":460},"步骤 3 模型编译",{"type":18,"tag":38,"props":462,"children":463},{},[464],{"type":24,"value":465},"mindspore.Model",{"type":18,"tag":38,"props":467,"children":468},{},[469],{"type":24,"value":470},"network：神经网络",{"type":18,"tag":38,"props":472,"children":473},{},[474],{"type":24,"value":475},"loss_fn：损失函数",{"type":18,"tag":38,"props":477,"children":478},{},[479],{"type":24,"value":480},"optimizer：优化器",{"type":18,"tag":38,"props":482,"children":483},{},[484],{"type":24,"value":485},"metrics：评估指标",{"type":18,"tag":119,"props":487,"children":489},{"code":488},"from mindspore import Model\n\n# 定义神经网络\nnet = nn.Dense(in_channels=3, out_channels=3, weight_init=1)\n# 定义损失函数\nloss = nn.SoftmaxCrossEntropyWithLogits()\n# 定义优化器\noptim = nn.Momentum(params=net.trainable_params(), learning_rate=0.1, momentum=0.9)\n# 模型编译\nmodel = Model(network = net, loss_fn=loss, optimizer=optim, metrics=None)\n",[490],{"type":18,"tag":124,"props":491,"children":492},{"__ignoreMap":7},[493],{"type":24,"value":488},{"type":18,"tag":38,"props":495,"children":496},{},[497],{"type":24,"value":498},"步骤 4 模型训练",{"type":18,"tag":38,"props":500,"children":501},{},[502],{"type":24,"value":503},"model.train",{"type":18,"tag":38,"props":505,"children":506},{},[507],{"type":24,"value":508},"epoch：训练次数",{"type":18,"tag":38,"props":510,"children":511},{},[512],{"type":24,"value":513},"train_dataset ：训练集",{"type":18,"tag":119,"props":515,"children":517},{"code":516},"# model.train(epoch=10, train_dataset=train_dataset)  # train_dataset 是传入参数\n",[518],{"type":18,"tag":124,"props":519,"children":520},{"__ignoreMap":7},[521],{"type":24,"value":516},{"type":18,"tag":38,"props":523,"children":524},{},[525],{"type":24,"value":526},"步骤 5 模型评估",{"type":18,"tag":38,"props":528,"children":529},{},[530],{"type":24,"value":531},"model.eval",{"type":18,"tag":38,"props":533,"children":534},{},[535],{"type":24,"value":536},"valid_dataset：测试集",{"type":18,"tag":119,"props":538,"children":540},{"code":539},"# model.eval(valid_dataset=test_dataset)  # test_dataset 是传入参数\n",[541],{"type":18,"tag":124,"props":542,"children":543},{"__ignoreMap":7},[544],{"type":24,"value":539},{"type":18,"tag":19,"props":546,"children":548},{"id":547},"_3-实验总结",[549],{"type":24,"value":550},"3 实验总结",{"type":18,"tag":38,"props":552,"children":553},{},[554],{"type":24,"value":555},"本实验介绍了MindSpore的数据结构与类型，以及MindSpore搭建神经网络用到的基础模块，让学员学会如何加载数据集，搭建神经网络，训练和评估模型等，从易到难，由浅入深，让学员熟悉MindSpore的基础用法，掌握MindSpore开发的简单流程。",{"title":7,"searchDepth":557,"depth":557,"links":558},4,[559,561,562,563,564,565,566,567],{"id":33,"depth":560,"text":36},2,{"id":45,"depth":560,"text":48},{"id":66,"depth":560,"text":69},{"id":77,"depth":560,"text":80},{"id":94,"depth":560,"text":97},{"id":269,"depth":560,"text":272},{"id":316,"depth":560,"text":319},{"id":401,"depth":560,"text":404},"markdown","content:technology-blogs:zh:3042.md","content","technology-blogs/zh/3042.md","technology-blogs/zh/3042","md",1776506125681]