[{"data":1,"prerenderedAt":106},["ShallowReactive",2],{"content-query-SJhlXgdP7l":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":100,"_id":101,"_source":102,"_file":103,"_stem":104,"_extension":105},"/technology-blogs/zh/3269","zh",false,"","mindspore安装及案例测试","1、mindspore安装","2024-08-02","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/11/28/34d22e34aba74d7abdff72621383169c.png","technology-blogs","安装",{"type":15,"children":16,"toc":94},"root",[17,24,30,36,44,51,56,66,73,79,87],{"type":18,"tag":19,"props":20,"children":21},"element","h1",{"id":8},[22],{"type":23,"value":8},"text",{"type":18,"tag":25,"props":26,"children":28},"h2",{"id":27},"_1mindspore安装",[29],{"type":23,"value":9},{"type":18,"tag":31,"props":32,"children":33},"p",{},[34],{"type":23,"value":35},"在官网选择对应的版本，电脑有GPU的可以安装Cuda，基础版通过pip安装cpu",{"type":18,"tag":31,"props":37,"children":38},{},[39],{"type":18,"tag":40,"props":41,"children":43},"img",{"alt":7,"src":42},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/08/16/90168f0a90d34331817d59b22c4888da.png",[],{"type":18,"tag":31,"props":45,"children":46},{},[47],{"type":18,"tag":40,"props":48,"children":50},{"alt":7,"src":49},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/08/16/eb545f9c0d7a4b2194b87655a7dc54ca.png",[],{"type":18,"tag":31,"props":52,"children":53},{},[54],{"type":23,"value":55},"验证是否安装成功",{"type":18,"tag":57,"props":58,"children":60},"pre",{"code":59}," python -c \"import mindspore;mindspore.set_context(device_target='CPU');mindspore.run_check()\"\n",[61],{"type":18,"tag":62,"props":63,"children":64},"code",{"__ignoreMap":7},[65],{"type":23,"value":59},{"type":18,"tag":31,"props":67,"children":68},{},[69],{"type":18,"tag":40,"props":70,"children":72},{"alt":7,"src":71},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/08/16/f3349716fd324f238fafc1e581714d2d.png",[],{"type":18,"tag":25,"props":74,"children":76},{"id":75},"_2mindspore案例测试",[77],{"type":23,"value":78},"2、mindspore案例测试",{"type":18,"tag":57,"props":80,"children":82},{"code":81},"from mindvision.dataset import Mnist  \n  \ndownload_train = Mnist(path=\"./mnist\",split=\"train\",batch_size=32,shuffle=True,resize=32,download=True)  \ndownload_eval = Mnist(path=\"./mnist\",split=\"test\",batch_size=32,shuffle=True,resize=32,download=True)  \n  \ndataset_train = download_train.run()  \ndataset_eval=download_eval.run()\n\nfrom mindvision.classification.models import lenet  \n  \nnetwork = lenet(num_classes=10)\n\nimport mindspore.nn as nn  \nfrom mindspore.train import Model  \n  \nnet_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True,reduction='mean')  \n  \nnet_opt = nn.Momentum(network.trainable_params(),learning_rate=0.01,momentum=0.9)\n\nfrom mindspore.train.callback import ModelCheckpoint,CheckpointConfig  \n  \nconfig_ck = CheckpointConfig(save_checkpoint_steps=1875,keep_checkpoint_max=10)  \n  \nckpoint = ModelCheckpoint(prefix=\"lenet\",directory=\"./lenet\",config=config_ck)\n\nfrom mindvision.engine.callback import LossMonitor  \n  \nmodel = Model(network,loss_fn=net_loss,optimizer=net_opt,metrics={'acc'})  \n  \nmodel.train(1,dataset_train,callbacks={ckpoint,LossMonitor(0.01)})\n\nacc = model.eval(dataset_eval)  \nprint(\"{}\".format(acc))\n\nfrom mindspore import load_checkpoint,load_param_into_net  \n  \nparam_dict =load_checkpoint(\"./lenet/lenet-1_1875.ckpt\")  \n  \nload_param_into_net(network,param_dict)\n\nimport numpy as np  \nfrom mindspore import Tensor  \nimport matplotlib.pyplot as plt  \nmnist= Mnist(\"./mnist\",split=\"test\",batch_size=6,resize=32)  \ndataset_infer =mnist.run()  \nde_test =dataset_infer.create_dict_iterator()  \ndata = next(de_test)  \nimages= data[\"image\"].asnumpy()  \nlabels= data[\"label\"].asnumpy()  \nplt.figure()  \nfor i in range(1,7):  \n    plt.subplot(2,3,i)  \n    plt.imshow(images[i-1][0],interpolation=\"None\",cmap=\"gray\")  \nplt.show()  \n  \noutput = model.predict(Tensor(data['image']))  \npredicted=np.argmax(output.asnumpy(),axis=1)  \nprint(f'Predicted:\"{predicted}\",Actual:\"{labels}\"')\n",[83],{"type":18,"tag":62,"props":84,"children":85},{"__ignoreMap":7},[86],{"type":23,"value":81},{"type":18,"tag":31,"props":88,"children":89},{},[90],{"type":18,"tag":40,"props":91,"children":93},{"alt":7,"src":92},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/08/16/25b4c47da31b438c9dc88f0253b5b3ab.png",[],{"title":7,"searchDepth":95,"depth":95,"links":96},4,[97,99],{"id":27,"depth":98,"text":9},2,{"id":75,"depth":98,"text":78},"markdown","content:technology-blogs:zh:3269.md","content","technology-blogs/zh/3269.md","technology-blogs/zh/3269","md",1776506128445]