00-1 开发环境介绍
欢迎体验Jupyter云上开发环境此开发环境是使用的是Jupyter Lab 3.x并集成了实用的插件。您可以在Jupyter Lab上灵活运行调试代码和编写文档Jupyter Lab集成了很多编辑器例如Jupyter笔记本、文本编辑器、终端和自定义组件。更多的详细介绍你可以查看 JupyterLab官网文档介绍。接下来跟我来一起沉浸式地探索Jupyter Lab吧❗若您不熟悉Jupyter Lab, 您可以先阅读Jupyter-Document.ipynb文件熟悉Jupyter的基本操作昇思MindSpore介绍昇思MindSpore是一个全场景深度学习框架旨在实现易开发、高效执行、全场景统一部署三大目标。其中易开发表现为API友好调试难度低高效执行包括计算效率、数据预处理效率和分布式训练效率全场景则指框架同时支持云、边缘以及端侧场景。MindSpore整体架构如下模型套件为开发者提供开箱即用的模型和开发套件如大模型套件MindSpore Transformers、MindSpore ONE以及科学计算库等热点研究领域拓展库深度学习科学计算为开发者提供AI模型开发所需各类Python接口最大化保持开发者在Python生态开发的使用习惯核心作为AI框架的核心构建Tensor数据结构、基础运算算子Operator、自动求导autograd模块、并行计算Parallel模块、编译compile能力以及runtime运行时管理模块。下面以MindSpore Transformers为例快速体验大模型的使用。快速体验平台操作快速体验推理MindSpore MindFormers套件的目标是构建一个大模型预训练、微调、评测、推理、部署的全流程开发套件提供业内主流的Transformer类大语言模型Large Language Models, LLMs和多模态理解模型Multimodal Models, MMs。期望帮助用户轻松地实现大模型全流程开发。MindSpore Transformers套件基于MindSpore内置的多维混合并行技术和组件化设计具备如下特点一键启动模型单卡或多卡预训练、微调、评测、推理、部署流程提供丰富的多维混合并行能力可供灵活易用地进行个性化配置大模型训推系统级深度优化原生支持超大规模集群高效训推故障快速恢复支持任务组件配置化开发。任意模块可通过统一配置进行使能包括模型网络、优化器、学习率策略等提供训练精度/性能监控指标实时可视化能力等。MindSpore Transformers-版本1.5.0支持的模型列表如下模型名支持规格模型类型CodeLlama34B稠密LLMCogVLM2-Image19BMMCogVLM2-Video13BMMDeepSeek-V3671B稀疏LLMDeepSeek-V2236B稀疏LLMDeepSeek-Coder-V1.57B稠密LLMDeepSeek-Coder33B稠密LLMGLM49B稠密LLMGLM3-32K6B稠密LLMGLM36B稠密LLMInternLM27B/20B稠密LLMLlama3.23B稠密LLMLlama3.2-Vision11BMMLlama3.18B/70B稠密LLMLlama38B/70B稠密LLMLlama27B/13B/70B稠密LLMMixtral8x7B稀疏LLMQwen2.50.5B/1.5B/7B/14B/32B/72B稠密LLMQwen20.5B/1.5B/7B/57B/57B-A14B/72B稠密/稀疏LLMQwen1.50.5B/1.8B/4B/7B/14B/72B稠密LLMQwen-VL9.6BMMTeleChat27B/35B/115B稠密LLMTeleChat7B/12B/52B稠密LLMWhisper1.5BMMYi6B/34B稠密LLMYiZhao12B稠密LLM如果感兴趣可以进入下面代码仓一键三连哦MindSpore Transformers仓MindSpore仓你可以CtrlEnter运行下面代码或者使用菜单栏的三角形运行下面的Cell注意一定要按顺序执行。使用 vit 进行图像分类%%capture captured_output# 实验环境已经预装了mindspore2.6.0如需更换mindspore版本可更改下面 MINDSPORE_VERSION 变量!pip uninstall mindspore-y%env MINDSPORE_VERSION2.6.0!pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/${MINDSPORE_VERSION}/MindSpore/unified/aarch64/mindspore-${MINDSPORE_VERSION}-cp39-cp39-linux_aarch64.whl--trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com-i https://pypi.tuna.tsinghua.edu.cn/simple# 查看当前 mindspore 版本!pip show mindsporeName: mindspore Version: 2.8.0 Summary: MindSpore is a new open source deep learning training/inference framework that could be used for mobile, edge and cloud scenarios. Home-page: https://www.mindspore.cn Author: The MindSpore Authors Author-email: contactmindspore.cn License: Apache 2.0 Location: /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages Requires: asttokens, astunparse, dill, numpy, packaging, pillow, protobuf, psutil, safetensors, scipy Required-by:# 控制MindSpore日志的级别, 0-DEBUG、1-INFO、2-WARNING、3-ERROR、4-EXCEPTIONimportos os.environ[GLOG_v]3%%capture captured_output# 安装 mindformers 包!pip install-i https://repo.huaweicloud.com/repository/pypi/simple/mindformers1.2.0MindFormers大模型套件面向任务设计pipeline推理接口旨在让用户可以便捷地体验不同AI领域的大模型在线推理服务当前已集成10任务的推理流程。使用MindFormers预置任务和模型开发一个推理流# 若出现ValueError: Failed to read the checkpoint file . May not have permission to read it, please check the correct of the file. 请将下面注释放开#!rm -rf checkpoint_download/vit/frommindformers.pipelineimportpipelinefrommindformers.tools.image_toolsimportload_imagefromPILimportImageimportmatplotlib.pyplotasplt pipeline_taskpipeline(image_classification,modelvit_base_p16)/home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages/numpy/core/getlimits.py:549: UserWarning: The value of the smallest subnormal for class numpy.float64 type is zero. setattr(self, word, getattr(machar, word).flat[0]) /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages/numpy/core/getlimits.py:89: UserWarning: The value of the smallest subnormal for class numpy.float64 type is zero. return self._float_to_str(self.smallest_subnormal) /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages/numpy/core/getlimits.py:549: UserWarning: The value of the smallest subnormal for class numpy.float32 type is zero. setattr(self, word, getattr(machar, word).flat[0]) /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages/numpy/core/getlimits.py:89: UserWarning: The value of the smallest subnormal for class numpy.float32 type is zero. return self._float_to_str(self.smallest_subnormal) 2025-02-22 16:46:50,265 - mindformers[mindformers/tools/download_tools.py:97] - INFO - Start download /home/mindspore/work/checkpoint_download/vit/vit_base_p16.ckpt Downloading: 346MB [00:22, 15.1MB/s] 2025-02-22 16:47:13,254 - mindformers[mindformers/tools/download_tools.py:110] - INFO - Download completed!, times: 23.27s 2025-02-22 16:47:13,258 - mindformers[mindformers/models/modeling_utils.py:1524] - INFO - start to read the ckpt file: 346282549 2025-02-22 16:47:14,124 - mindformers[mindformers/models/modeling_utils.py:1529] - INFO - weights in ./checkpoint_download/vit/vit_base_p16.ckpt are loadedfromdownloadimportdownload# 下载测试图片urlhttps://obs-xihe-beijing4.obs.cn-north-4.myhuaweicloud.com/jupyter/images/daisy.jpgdownload(url,daisy.jpg,replaceTrue)Downloading data from https://obs-xihe-beijing4.obs.cn-north-4.myhuaweicloud.com/jupyter/images/daisy.jpg (314 kB) file_sizes: 100%|████████████████████████████| 321k/321k [00:0000:00, 7.10MB/s] Successfully downloaded file to daisy.jpg daisy.jpgfromPILimportImageimportmatplotlib.pyplotasplt test_pngdaisy.jpgpictureImage.open(test_png)plt.figure(image)plt.imshow(picture)plt.show()imgload_image(test_png)pipeline_resultpipeline_task(img,top_k3)pipeline_result--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[6], line 1 ---- 1 img load_image(test_png) 2 pipeline_result pipeline_task(img, top_k3) 3 pipeline_result NameError: name load_image is not defined使用 BERT 进行中文命名实体识别# pipeline接口开启快速推理frommindformers.pipelineimportTokenClassificationPipelinefrommindformersimportAutoTokenizer,BertForTokenClassification,AutoConfigfrommindformers.dataset.labelsimportcluener_labels id2label{label_id:labelforlabel_id,labelinenumerate(cluener_labels)}tokenizerAutoTokenizer.from_pretrained(tokcls_bert_base_chinese_cluener,)tokcls_cluener_configAutoConfig.from_pretrained(tokcls_bert_base_chinese_cluener,)# This is a known issue, you need to specify batch size equal to 1 when creating bert model.tokcls_cluener_config.batch_size1modelBertForTokenClassification(tokcls_cluener_config)tokcls_pipelineTokenClassificationPipeline(tasktoken_classification,modelmodel,id2labelid2label,tokenizertokenizer,max_lengthmodel.config.seq_length,paddingmax_length)2025-02-22 16:47:29,371 - mindformers[mindformers/models/auto/tokenization_auto.py:396] - INFO - default yaml config in ./checkpoint_download/tokcls/tokcls_bert_base_chinese_cluener.yaml is used. 2025-02-22 16:47:29,372 - mindformers[mindformers/models/auto/tokenization_auto.py:328] - INFO - Config in the yaml file ./checkpoint_download/tokcls/tokcls_bert_base_chinese_cluener.yaml are used for tokenizer building. 2025-02-22 16:47:29,411 - mindformers[mindformers/models/auto/tokenization_auto.py:335] - INFO - Load the tokenizer name BertTokenizer from the ./checkpoint_download/tokcls/tokcls_bert_base_chinese_cluener.yaml 2025-02-22 16:47:29,412 - mindformers[mindformers/models/tokenization_utils_base.py:1949] - INFO - Download the vocab from the url https://ascend-repo-modelzoo.obs.cn-east-2.myhuaweicloud.com/XFormer_for_mindspore/bert/bert_base_chinese/vocab.txt to ./checkpoint_download/tokcls/vocab.txt. 2025-02-22 16:47:29,671 - mindformers[mindformers/tools/download_tools.py:97] - INFO - Start download /home/mindspore/work/checkpoint_download/tokcls/vocab.txt Downloading: 110kB [00:00, 2.01MB/s] 2025-02-22 16:47:29,729 - mindformers[mindformers/tools/download_tools.py:110] - INFO - Download completed!, times: 0.32s 2025-02-22 16:47:29,768 - mindformers[mindformers/models/tokenization_utils_base.py:1886] - WARNING - Cant find the tokenizer_config.json in the file_dict. The content of file_dict is : {} 2025-02-22 16:47:29,769 - mindformers[mindformers/models/tokenization_utils_base.py:1893] - INFO - build tokenizer class name is: BertTokenizer using args {cls_token: [CLS], mask_token: [MASK], pad_token: [PAD], sep_token: [SEP], unk_token: [UNK], is_tokenize_char: True, do_lower_case: False, checkpoint_name_or_path: tokcls_bert_base_chinese, vocab_file: ./checkpoint_download/tokcls/vocab.txt}. 2025-02-22 16:47:29,813 - mindformers[mindformers/models/auto/tokenization_auto.py:413] - INFO - BertTokenizer Tokenizer built successfully! 2025-02-22 16:47:32,208 - mindformers[mindformers/tools/download_tools.py:97] - INFO - Start download /home/mindspore/work/checkpoint_download/tokcls/tokcls_bert_base_chinese_cluener.ckpt Downloading: 1.23GB [01:30, 13.5MB/s] 2025-02-22 16:49:03,003 - mindformers[mindformers/tools/download_tools.py:110] - INFO - Download completed!, times: 91.10s 2025-02-22 16:49:03,006 - mindformers[mindformers/models/modeling_utils.py:1524] - INFO - start to read the ckpt file: 1227549758 2025-02-22 16:49:05,831 - mindformers[mindformers/models/modeling_utils.py:1529] - INFO - weights in ./checkpoint_download/tokcls/tokcls_bert_base_chinese_cluener.ckpt are loadedinput_data[小明作为开发人员去北京参加华为开发者大会。]resultstokcls_pipeline(input_data)print(results).[[{entity_group: position, start: 4, end: 6, score: 0.72438, word: 开发人}, {entity_group: address, start: 9, end: 10, score: 0.93827, word: 北京}, {entity_group: company, start: 13, end: 14, score: 0.54733, word: 华为}]]快速体验训练这里以Lenet5实现手写数字体为案例基本介绍LeNet描述LeNet是1998年提出的一种典型的卷积神经网络。它被用于数字识别并取得了巨大的成功。论文 Y.Lecun, L.Bottou, Y.Bengio, P.Haffner.Gradient-Based Learning Applied to Document Recognition.Proceedings of the IEEE.1998.模型架构按照LeNet的网络结构LeNet除去输入层共有7层其中有2个卷积层2个子采样层3个全连接层。下载MNIST数据集MNIST数据集是由10类28∗28的灰度图片组成训练数据集包含60000张图片测试数据集包含10000张图片。下载完成后下载完成后的目录结构如下└── MNIST_Data ├── test # 测试集 │ ├── t10k-images-idx3-ubyte (10000个测试图片) │ └── t10k-labels-idx1-ubyte (10000个测试标签) │── train # 训练集 │ ├── train-images-idx3-ubyte (60000个训练图片) │ └── train-lables-idx1-ubyte (60000个训练标签) └── README.mdfromdownloadimportdownload url(https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/MNIST_Data.zip)pathdownload(url,./,kindzip,replaceTrue)Downloading data from https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/MNIST_Data.zip (10.3 MB) file_sizes: 100%|███████████████████████████| 10.8M/10.8M [00:0000:00, 120MB/s] Extracting zip file... Successfully downloaded / unzipped to ./创建模型MindSpore Vision套件提供了LeNet网络模型接口lenet 定义网络模型如下frommindvision.classification.modelsimportlenet networklenet(num_classes10,pretrainedFalse)定义损失函数和优化器要训练神经网络模型需要定义损失函数和优化器函数。损失函数这里使用交叉熵损失函数SoftmaxCrossEntropyWithLogits。优化器这里使用Momentum。# 超参设置learning_rate0.01momentum0.9batch_size32num_epochs5epoch_size10importmindspore.nnasnn# 定义损失函数net_lossnn.SoftmaxCrossEntropyWithLogits(sparseTrue,reductionmean)# 定义优化器函数net_optnn.Momentum(network.trainable_params(),learning_ratelearning_rate,momentummomentum)模型训练frommindspore.trainimportModelfrommindvision.classification.datasetimportMnistfrommindspore.train.callbackimportModelCheckpoint,CheckpointConfigfrommindvision.engine.callbackimportLossMonitor data_url./MNIST_Dataoutput_path./lenet# 初始化模型参数modelModel(network,loss_fnnet_loss,optimizernet_opt,metrics{accuracy})deftrain():# 定义训练数据集download_trainMnist(pathdata_url,splittrain,batch_sizebatch_size,repeat_num1,shuffleTrue,resize32,downloadTrue,)dataset_traindownload_train.run()# 设置模型保存参数模型训练保存参数的step为1875config_ckCheckpointConfig(save_checkpoint_steps1875,keep_checkpoint_max10)# 应用模型保存参数ckpointModelCheckpoint(prefixlenet,directoryoutput_path,configconfig_ck)# 训练网络模型并保存为lenet-1_1875.ckpt文件model.train(epoch_size,dataset_train,callbacks[ckpoint,LossMonitor(0.01,1875)])# 执行训练train()....Epoch:[ 0/ 10], step:[ 1875/ 1875], loss:[0.055/0.361], time:19.343 ms, lr:0.01000 Epoch time: 54393.399 ms, per step time: 29.010 ms, avg loss: 0.361 Epoch:[ 1/ 10], step:[ 1875/ 1875], loss:[0.021/0.061], time:18.822 ms, lr:0.01000 Epoch time: 18903.794 ms, per step time: 10.082 ms, avg loss: 0.061 Epoch:[ 2/ 10], step:[ 1875/ 1875], loss:[0.022/0.044], time:17.664 ms, lr:0.01000 Epoch time: 18859.462 ms, per step time: 10.058 ms, avg loss: 0.044 Epoch:[ 3/ 10], step:[ 1875/ 1875], loss:[0.001/0.035], time:18.250 ms, lr:0.01000 Epoch time: 19054.932 ms, per step time: 10.163 ms, avg loss: 0.035 Epoch:[ 4/ 10], step:[ 1875/ 1875], loss:[0.009/0.029], time:18.280 ms, lr:0.01000 Epoch time: 19356.754 ms, per step time: 10.324 ms, avg loss: 0.029 Epoch:[ 5/ 10], step:[ 1875/ 1875], loss:[0.101/0.023], time:18.708 ms, lr:0.01000 Epoch time: 19428.140 ms, per step time: 10.362 ms, avg loss: 0.023 Epoch:[ 6/ 10], step:[ 1875/ 1875], loss:[0.098/0.020], time:17.957 ms, lr:0.01000 Epoch time: 19325.511 ms, per step time: 10.307 ms, avg loss: 0.020 Epoch:[ 7/ 10], step:[ 1875/ 1875], loss:[0.000/0.016], time:18.525 ms, lr:0.01000 Epoch time: 19252.076 ms, per step time: 10.268 ms, avg loss: 0.016 Epoch:[ 8/ 10], step:[ 1875/ 1875], loss:[0.005/0.015], time:18.509 ms, lr:0.01000 Epoch time: 19266.987 ms, per step time: 10.276 ms, avg loss: 0.015 Epoch:[ 9/ 10], step:[ 1875/ 1875], loss:[0.005/0.012], time:17.811 ms, lr:0.01000 Epoch time: 19322.312 ms, per step time: 10.305 ms, avg loss: 0.012训练过程中会打印loss值loss值会波动但总体来说loss值会逐步减小精度逐步提高。每个人运行的loss值有一定随机性不一定完全相同。模型验证及推理通过模型运行测试数据集得到的结果验证模型的泛化能力1.使用model.eval接口读入测试数据集。2.使用保存后的模型参数进行推理。测试集验证精确度defevaluate():# 定义训练数据集download_testMnist(pathdata_url,splittest,batch_sizebatch_size,repeat_num1,shuffleTrue,resize32,downloadTrue,)dataset_testdownload_test.run()accmodel.eval(dataset_test)print({}.format(acc))evaluate(){accuracy: 0.9904847756410257}可以在打印信息中看出模型精度数据示例中精度数据达到95%以上模型质量良好。随着网络迭代次数增加模型精度会进一步提高。模型推理我们使用生成的模型进行单个图片数据的分类预测具体步骤如下importnumpyasnpfrommindsporeimportTensorimportmatplotlib.pyplotasplt mnistMnist(data_url,splittrain,batch_size6,resize32)dataset_infermnist.run()ds_testdataset_infer.create_dict_iterator()datanext(ds_test)imagesdata[image].asnumpy()labelsdata[label].asnumpy()plt.figure()foriinrange(1,7):plt.subplot(2,3,i)plt.imshow(images[i-1][0],interpolationNone,cmapgray)plt.show()# 使用函数model.predict预测image对应分类outputmodel.predict(Tensor(data[image]))predictednp.argmax(output.asnumpy(),axis1)# 输出预测分类与实际分类print(fPredicted: {predicted}, Actual: {labels})Predicted: [3 7 9 8 6 1], Actual: [3 7 9 8 6 1]更多体验持续更新中…