提交 8e84c5d4 编写于 作者: M mindspore-ci-bot 提交者: Gitee

!161 fix README.md text format

Merge pull request !161 from lvmingfu/lmf-docs
......@@ -49,4 +49,4 @@
| 教程名称 | 内容描述
| :----------- |:------
| [quick_start.ipynb](./quick_start.ipynb) | 通过该文件,你可更容易地理解各个功能模块的具体作用,学习到数据集查看及数据集图形展示方法,了解到数据集是如何通过训练生成模型;也可以通过LeNet计算图的展示,了解具体结构和参数作用;可以学习使用自定义回调函数来了解训练过程模型的变化,通过训练过程loss值与训练步数的变化图,模型精度与训练步数的变化图,更容易的理解训练对机器学习产生的意义,还能学习将训练出来的模型应用到手写图片的预测与分类上。
\ No newline at end of file
| [quick_start.ipynb](./quick_start.ipynb) | - 从数据集到模型验证的全过程解读 <br/> - 体验教程中各功能模块的使用说明 <br/> - 数据集图形化展示 <br/> - 了解LeNet5具体结构和参数作用 <br/> - 学习使用自定义回调函数 <br/> - loss值与训练步数的变化图 <br/> - 模型精度与训练步数的变化图 <br/> - 使用模型应用到手写图片的预测与分类上
\ No newline at end of file
......@@ -106,7 +106,7 @@
" test_path = \"./MNIST_Data/test/\"\n",
" train_path_check = os.path.exists(train_path)\n",
" test_path_check = os.path.exists(test_path)\n",
" if train_path_check == False and test_path_check ==False:\n",
" if train_path_check == False and test_path_check == False:\n",
" os.makedirs(train_path)\n",
" os.makedirs(test_path)\n",
" train_url = {\"http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz\", \"http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz\"}\n",
......@@ -116,7 +116,7 @@
" url_parse = urlparse(url)\n",
" # split the file name from url\n",
" file_name = os.path.join(train_path,url_parse.path.split('/')[-1])\n",
" if not os.path.exists(file_name.replace('.gz','')):\n",
" if not os.path.exists(file_name.replace('.gz', '')):\n",
" file = urllib.request.urlretrieve(url, file_name)\n",
" unzipfile(file_name)\n",
" os.remove(file_name)\n",
......@@ -125,7 +125,7 @@
" url_parse = urlparse(url)\n",
" # split the file name from url\n",
" file_name = os.path.join(test_path,url_parse.path.split('/')[-1])\n",
" if not os.path.exists(file_name.replace('.gz','')):\n",
" if not os.path.exists(file_name.replace('.gz', '')):\n",
" file = urllib.request.urlretrieve(url, file_name)\n",
" unzipfile(file_name)\n",
" os.remove(file_name)\n",
......@@ -180,11 +180,11 @@
"import numpy as np\n",
"import mindspore.dataset as ds\n",
"\n",
"context.set_context(mode=context.GRAPH_MODE,device_target=\"CPU\") # Windows version, set to use CPU for graph calculation\n",
"context.set_context(mode=context.GRAPH_MODE, device_target=\"CPU\") # Windows version, set to use CPU for graph calculation\n",
"train_data_path = \"./MNIST_Data/train\"\n",
"test_data_path = \"./MNIST_Data/test\"\n",
"mnist_ds = ds.MnistDataset(train_data_path) # Load training dataset\n",
"print('The type of mnist_ds:',type(mnist_ds))\n",
"print('The type of mnist_ds:', type(mnist_ds))\n",
"print(\"Number of pictures contained in the mnist_ds:\",mnist_ds.get_dataset_size()) # 60000 pictures in total\n",
"\n",
"dic_ds = mnist_ds.create_dict_iterator() # Convert dataset to dictionary type\n",
......@@ -192,12 +192,12 @@
"img = item[\"image\"]\n",
"label = item[\"label\"]\n",
"\n",
"print(\"The item of mnist_ds:\",item.keys()) # Take a single data to view the data structure, including two keys, image and label\n",
"print(\"Tensor of image in item:\",img.shape) # View the tensor of image (28,28,1)\n",
"print(\"The label of item:\",label)\n",
"print(\"The item of mnist_ds:\", item.keys()) # Take a single data to view the data structure, including two keys, image and label\n",
"print(\"Tensor of image in item:\", img.shape) # View the tensor of image (28,28,1)\n",
"print(\"The label of item:\", label)\n",
"\n",
"plt.imshow(np.squeeze(img))\n",
"plt.title(\"number:%s\"%item[\"label\"])\n",
"plt.title(\"number:%s\"% item[\"label\"])\n",
"plt.show()"
]
},
......@@ -313,8 +313,8 @@
"metadata": {},
"outputs": [],
"source": [
"datas = create_dataset(train_data_path) # Process the train dataset\n",
"print('Number of groups in the dataset:',datas.get_dataset_size()) # Number of query dataset groups"
"datas = create_dataset(train_data_path) # Process the train dataset\n",
"print('Number of groups in the dataset:', datas.get_dataset_size()) # Number of query dataset groups"
]
},
{
......@@ -327,15 +327,17 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"data = datas.create_dict_iterator().get_next() # Take a set of datasets\n",
"print(data.keys())\n",
"images = data[\"image\"] # Take out the image data in this dataset\n",
"labels = data[\"label\"] # Take out the label (subscript) of this data set\n",
"print('Tensor of image:',images.shape) # Query the tensor of images in each dataset (32,1,32,32)\n",
"print('labels:',labels)"
"print('Tensor of image:', images.shape) # Query the tensor of images in each dataset (32,1,32,32)\n",
"print('labels:', labels)"
]
},
{
......@@ -353,11 +355,11 @@
"source": [
"count = 1\n",
"for i in images:\n",
" plt.subplot(4,8,count) \n",
" plt.subplot(4, 8, count) \n",
" plt.imshow(np.squeeze(i))\n",
" plt.title('num:%s'%labels[count-1])\n",
" plt.xticks([])\n",
" count+=1\n",
" count += 1\n",
" plt.axis(\"off\")\n",
"plt.show() # Print a total of 32 pictures in the group"
]
......@@ -442,9 +444,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"使用MindSpore定义神经网络需要继承mindspore.nn.cell.CellCell是所有神经网络(Conv2d等)的基类。\n",
"使用MindSpore定义神经网络需要继承mindspore.nn.cell.CellCell是所有神经网络(Conv2d等)的基类。\n",
"\n",
"神经网络的各层需要预先在\\_\\_init\\_\\_()方法中定义,然后通过定义construct()方法来完成神经网络的前向构造按照LeNet5的网络结构,定义网络各层如下:"
"神经网络的各层需要预先在\\_\\_init\\_\\_()方法中定义,然后通过定义construct()方法来完成神经网络的前向构造按照LeNet5的网络结构,定义网络各层如下:"
]
},
{
......@@ -545,19 +547,19 @@
"from mindspore.nn.metrics import Accuracy\n",
"from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits\n",
"\n",
"def train_net(model, epoch_size, mnist_path, repeat_size, ckpoint_cb,lmf_info):\n",
"def train_net(model, epoch_size, mnist_path, repeat_size, ckpoint_cb, step_loss_info):\n",
" \"\"\"Define the training method.\"\"\"\n",
" print(\"============== Starting Training ==============\")\n",
" # load training dataset\n",
" ds_train = create_dataset(os.path.join(mnist_path, \"train\"), 32, repeat_size)\n",
" model.train(epoch_size, ds_train, callbacks=[ckpoint_cb, LossMonitor(),lmf_info], dataset_sink_mode=False)"
" model.train(epoch_size, ds_train, callbacks=[ckpoint_cb, LossMonitor(), step_loss_info], dataset_sink_mode=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"自定义一个存储每一步训练的step和对应loss值的回调LMF_info函数,本函数继承了Callback类,可以自定义训练过程中的处理措施,非常方便,等训练完成后,可将数据绘图查看loss的变化情况。"
"自定义一个存储每一步训练的step和对应loss值的类Step_loss_info(),并继承了Callback类,可以自定义训练过程中的处理措施,非常方便,等训练完成后,可将数据绘图查看loss的变化情况。"
]
},
{
......@@ -567,7 +569,7 @@
"outputs": [],
"source": [
"# Custom callback function\n",
"class LMF_info(Callback):\n",
"class Step_loss_info(Callback):\n",
" def step_end(self, run_context):\n",
" cb_params = run_context.original_args()\n",
" # step_ Loss dictionary for saving loss value and step number information\n",
......@@ -613,7 +615,7 @@
"# define the loss function\n",
"net_loss = SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True, reduction='mean')\n",
"# define the model\n",
"model = Model(network, net_loss, net_opt,metrics={\"Accuracy\":Accuracy()} )#metrics={\"Accuracy\": Accuracy()}\n",
"model = Model(network, net_loss, net_opt, metrics={\"Accuracy\": Accuracy()} )\n",
"\n",
"\n",
"epoch_size = 1\n",
......@@ -624,12 +626,12 @@
"\n",
"ckpoint_cb = ModelCheckpoint(prefix=\"checkpoint_lenet\", config=config_ck)\n",
"# group layers into an object with training and evaluation features\n",
"step_loss = {\"step\":[],\"loss_value\":[]}\n",
"step_loss = {\"step\": [], \"loss_value\": []}\n",
"# step_ Loss dictionary for saving loss value and step number information\n",
"lmf_info=LMF_info()\n",
"step_loss_info = Step_loss_info()\n",
"# save the steps and loss value\n",
"repeat_size = 1\n",
"train_net(model, epoch_size, mnist_path, repeat_size, ckpoint_cb,lmf_info)\n"
"train_net(model, epoch_size, mnist_path, repeat_size, ckpoint_cb, step_loss_info)\n"
]
},
{
......@@ -649,14 +651,16 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"steps=step_loss[\"step\"]\n",
"steps = step_loss[\"step\"]\n",
"loss_value = step_loss[\"loss_value\"]\n",
"steps = list(map(int,steps))\n",
"loss_value = list(map(float,loss_value))\n",
"plt.plot(steps,loss_value,color=\"red\")\n",
"steps = list(map(int, steps))\n",
"loss_value = list(map(float, loss_value))\n",
"plt.plot(steps, loss_value, color=\"red\")\n",
"plt.xlabel(\"Steps\")\n",
"plt.ylabel(\"Loss_value\")\n",
"plt.title(\"Loss function value change chart\")\n",
......@@ -734,9 +738,9 @@
"source": [
"def acc_model_info(network, model, mnist_path, model_numbers):\n",
" \"\"\"Define the plot info method\"\"\"\n",
" step_list=[]\n",
" acc_list =[]\n",
" for i in range(1,model_numbers+1):\n",
" step_list = []\n",
" acc_list = []\n",
" for i in range(1, model_numbers+1):\n",
" # load the saved model for evaluation\n",
" param_dict = load_checkpoint(\"checkpoint_lenet-1_{}.ckpt\".format(str(i*125)))\n",
" # load parameter to the network\n",
......@@ -749,11 +753,11 @@
" return step_list,acc_list\n",
"\n",
"# Draw line chart according to training steps and model accuracy\n",
"l1,l2 = acc_model_info(network, model, mnist_path,15)\n",
"l1,l2 = acc_model_info(network, model, mnist_path, 15)\n",
"plt.xlabel(\"Model of Steps\")\n",
"plt.ylabel(\"Model accuracy\")\n",
"plt.title(\"Model accuracy variation chart\")\n",
"plt.plot(l1,l2,'red')\n",
"plt.plot(l1, l2, 'red')\n",
"plt.show()"
]
},
......@@ -809,23 +813,23 @@
"output =model.predict(Tensor(data['image']))\n",
"# The predict function returns the probability of 0-9 numbers corresponding to each picture\n",
"prb = output.asnumpy()\n",
"pred = np.argmax(output.asnumpy(),axis=1)\n",
"pred = np.argmax(output.asnumpy(), axis=1)\n",
"err_num = []\n",
"index = 1\n",
"for i in range(len(labels)):\n",
" plt.subplot(4,8,i+1)\n",
" color = 'blue' if pred[i]==labels[i] else 'red'\n",
" plt.title(\"pre:{}\".format(pred[i]),color = color)\n",
" plt.subplot(4, 8, i+1)\n",
" color = 'blue' if pred[i] == labels[i] else 'red'\n",
" plt.title(\"pre:{}\".format(pred[i]), color=color)\n",
" plt.imshow(np.squeeze(images[i]))\n",
" plt.axis(\"off\")\n",
" if color =='red':\n",
" index=0\n",
" if color == 'red':\n",
" index = 0\n",
" # Print out the wrong data identified by the current group\n",
" print(\"Row {}, column {} is incorrectly identified as {}, the correct value should be {}\".format(int(i/8)+1,i%8+1,pred[i],labels[i]),'\\n')\n",
" print(\"Row {}, column {} is incorrectly identified as {}, the correct value should be {}\".format(int(i/8)+1, i%8+1, pred[i], labels[i]), '\\n')\n",
"if index:\n",
" print(\"All the figures in this group are predicted correctly!\")\n",
"print(pred,\"<--Predicted figures\") # Print the numbers recognized by each group of pictures\n",
"print(labels,\"<--The right number\") # Print the subscript corresponding to each group of pictures\n",
"print(pred, \"<--Predicted figures\") # Print the numbers recognized by each group of pictures\n",
"print(labels, \"<--The right number\") # Print the subscript corresponding to each group of pictures\n",
"plt.show()"
]
},
......@@ -846,16 +850,16 @@
"source": [
"# define the pie drawing function of probability analysis\n",
"def plot_pie(prbs):\n",
" dict1={}\n",
" dict1 = {}\n",
" # Remove the negative number and build the dictionary dict1. The key is the number and the value is the probability value\n",
" for i in range(10):\n",
" if prbs[i]>0:\n",
" dict1[str(i)]=prbs[i]\n",
" if prbs[i] > 0:\n",
" dict1[str(i)] = prbs[i]\n",
"\n",
" label_list = dict1.keys() # Label of each part\n",
" size = dict1.values() # Size of each part\n",
" colors = [\"red\", \"green\",\"pink\",\"blue\",\"purple\",\"orange\",\"gray\"] # Building a round cake pigment Library\n",
" color = colors[:len(size)]# Color of each part\n",
" colors = [\"red\", \"green\", \"pink\", \"blue\", \"purple\", \"orange\", \"gray\"] # Building a round cake pigment Library\n",
" color = colors[: len(size)]# Color of each part\n",
" plt.pie(size, colors=color, labels=label_list, labeldistance=1.1, autopct=\"%1.1f%%\", shadow=False, startangle=90, pctdistance=0.6)\n",
" plt.axis(\"equal\") # Set the scale size of x-axis and y-axis to be equal\n",
" plt.legend()\n",
......@@ -864,7 +868,7 @@
" \n",
" \n",
"for i in range(2):\n",
" print(\"Figure {} probability of corresponding numbers [0-9]:\\n\".format(i+1),prb[i])\n",
" print(\"Figure {} probability of corresponding numbers [0-9]:\\n\".format(i+1), prb[i])\n",
" plot_pie(prb[i])"
]
},
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册