You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

02-LeNet5.ipynb 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# LeNet5\n",
  8. "\n",
  9. "LeNet 诞生于 1994 年,是最早的卷积神经网络之一,并且推动了深度学习领域的发展。自从 1988 年开始,在多次迭代后这个开拓性成果被命名为 LeNet5。LeNet5 的架构的提出是基于如下的观点:图像的特征分布在整张图像上,通过带有可学习参数的卷积,从而有效的减少了参数数量,能够在多个位置上提取相似特征。\n",
  10. "\n",
  11. "在LeNet5提出的时候,没有 GPU 帮助训练,甚至 CPU 的速度也很慢,因此,LeNet5的规模并不大。其包含七个处理层,每一层都包含可训练参数(权重),当时使用的输入数据是 $32 \\times 32$ 像素的图像。LeNet-5 这个网络虽然很小,但是它包含了深度学习的基本模块:卷积层,池化层,全连接层。它是其他深度学习模型的基础,这里对LeNet5进行深入分析和讲解,通过实例分析,加深对与卷积层和池化层的理解。"
  12. ]
  13. },
  14. {
  15. "cell_type": "markdown",
  16. "metadata": {},
  17. "source": [
  18. "定义网络为:"
  19. ]
  20. },
  21. {
  22. "cell_type": "code",
  23. "execution_count": null,
  24. "metadata": {},
  25. "outputs": [],
  26. "source": [
  27. "import torch\n",
  28. "from torch import nn\n",
  29. "import torch.nn.functional as F\n",
  30. "\n",
  31. "class LeNet5(nn.Module):\n",
  32. " def __init__(self):\n",
  33. " super(LeNet5, self).__init__()\n",
  34. " # 1-input channel, 6-output channels, 5x5-conv\n",
  35. " self.conv1 = nn.Conv2d(1, 6, 5)\n",
  36. " # 6-input channel, 16-output channels, 5x5-conv\n",
  37. " self.conv2 = nn.Conv2d(6, 16, 5)\n",
  38. " # 16x5x5-input, 120-output\n",
  39. " self.fc1 = nn.Linear(16 * 5 * 5, 120) \n",
  40. " # 120-input, 84-output\n",
  41. " self.fc2 = nn.Linear(120, 84)\n",
  42. " # 84-input, 10-output\n",
  43. " self.fc3 = nn.Linear(84, 10)\n",
  44. "\n",
  45. " def forward(self, x):\n",
  46. " x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))\n",
  47. " x = F.max_pool2d(F.relu(self.conv2(x)), (2, 2))\n",
  48. " x = torch.flatten(x, 1) # 将结果拉升成1维向量,除了批次的维度\n",
  49. " x = F.relu(self.fc1(x))\n",
  50. " x = F.relu(self.fc2(x))\n",
  51. " x = self.fc3(x)\n",
  52. " return x"
  53. ]
  54. },
  55. {
  56. "cell_type": "code",
  57. "execution_count": null,
  58. "metadata": {},
  59. "outputs": [],
  60. "source": [
  61. "net = LeNet5()\n",
  62. "print(net)"
  63. ]
  64. },
  65. {
  66. "cell_type": "code",
  67. "execution_count": null,
  68. "metadata": {},
  69. "outputs": [],
  70. "source": [
  71. "input = torch.randn(1, 1, 32, 32)\n",
  72. "out = net(input)\n",
  73. "print(out)"
  74. ]
  75. },
  76. {
  77. "cell_type": "code",
  78. "execution_count": null,
  79. "metadata": {},
  80. "outputs": [],
  81. "source": [
  82. "import numpy as np\n",
  83. "from torchvision.datasets import mnist\n",
  84. "from torch.utils.data import DataLoader\n",
  85. "from torchvision.datasets import mnist \n",
  86. "from torchvision import transforms as tfs\n",
  87. "from utils import train\n",
  88. "\n",
  89. "# 使用数据增强\n",
  90. "def data_tf(x):\n",
  91. " im_aug = tfs.Compose([\n",
  92. " tfs.Resize(32),\n",
  93. " tfs.ToTensor() #,\n",
  94. " #tfs.Normalize([0.5], [0.5])\n",
  95. " ])\n",
  96. " x = im_aug(x)\n",
  97. " return x\n",
  98. " \n",
  99. "train_set = mnist.MNIST('../../data/mnist', train=True, transform=data_tf, download=True) \n",
  100. "train_data = torch.utils.data.DataLoader(train_set, batch_size=64, shuffle=True)\n",
  101. "test_set = mnist.MNIST('../../data/mnist', train=False, transform=data_tf, download=True) \n",
  102. "test_data = torch.utils.data.DataLoader(test_set, batch_size=128, shuffle=False)\n",
  103. "\n"
  104. ]
  105. },
  106. {
  107. "cell_type": "code",
  108. "execution_count": null,
  109. "metadata": {},
  110. "outputs": [],
  111. "source": [
  112. "# 显示其中一个数据\n",
  113. "import matplotlib.pyplot as plt\n",
  114. "plt.imshow(train_set.data[0], cmap='gray')\n",
  115. "plt.title('%i' % train_set.targets[0])\n",
  116. "plt.colorbar()\n",
  117. "plt.show()"
  118. ]
  119. },
  120. {
  121. "cell_type": "code",
  122. "execution_count": null,
  123. "metadata": {},
  124. "outputs": [],
  125. "source": [
  126. "import matplotlib.pyplot as plt\n",
  127. "\n",
  128. "# 显示转化后的图像\n",
  129. "for im, label in train_data:\n",
  130. " print(im.shape)\n",
  131. " print(label.shape)\n",
  132. " \n",
  133. " img = im[0,0,:,:]\n",
  134. " lab = label[0]\n",
  135. " plt.imshow(img, cmap='gray')\n",
  136. " plt.title('%i' % lab)\n",
  137. " plt.colorbar()\n",
  138. " plt.show()\n",
  139. "\n",
  140. " print(im[0,0,:,:])\n",
  141. " break"
  142. ]
  143. },
  144. {
  145. "cell_type": "code",
  146. "execution_count": null,
  147. "metadata": {
  148. "scrolled": false
  149. },
  150. "outputs": [],
  151. "source": [
  152. "net = LeNet5()\n",
  153. "optimizer = torch.optim.Adam(net.parameters(), lr=1e-3)\n",
  154. "criterion = nn.CrossEntropyLoss()\n",
  155. "\n",
  156. "res = train(net, train_data, test_data, 20, \n",
  157. " optimizer, criterion,\n",
  158. " use_cuda=True)"
  159. ]
  160. },
  161. {
  162. "cell_type": "code",
  163. "execution_count": null,
  164. "metadata": {},
  165. "outputs": [],
  166. "source": [
  167. "import matplotlib.pyplot as plt\n",
  168. "%matplotlib inline\n",
  169. "\n",
  170. "plt.plot(res[0], label='train')\n",
  171. "plt.plot(res[2], label='valid')\n",
  172. "plt.xlabel('epoch')\n",
  173. "plt.ylabel('Loss')\n",
  174. "plt.legend(loc='best')\n",
  175. "plt.savefig('fig-res-lenet5-train-validate-loss.pdf')\n",
  176. "plt.show()\n",
  177. "\n",
  178. "plt.plot(res[1], label='train')\n",
  179. "plt.plot(res[3], label='valid')\n",
  180. "plt.xlabel('epoch')\n",
  181. "plt.ylabel('Acc')\n",
  182. "plt.legend(loc='best')\n",
  183. "plt.savefig('fig-res-lenet5-train-validate-acc.pdf')\n",
  184. "plt.show()"
  185. ]
  186. }
  187. ],
  188. "metadata": {
  189. "kernelspec": {
  190. "display_name": "Python 3",
  191. "language": "python",
  192. "name": "python3"
  193. },
  194. "language_info": {
  195. "codemirror_mode": {
  196. "name": "ipython",
  197. "version": 3
  198. },
  199. "file_extension": ".py",
  200. "mimetype": "text/x-python",
  201. "name": "python",
  202. "nbconvert_exporter": "python",
  203. "pygments_lexer": "ipython3",
  204. "version": "3.7.9"
  205. }
  206. },
  207. "nbformat": 4,
  208. "nbformat_minor": 2
  209. }

机器学习越来越多应用到飞行器、机器人等领域,其目的是利用计算机实现类似人类的智能,从而实现装备的智能化与无人化。本课程旨在引导学生掌握机器学习的基本知识、典型方法与技术,通过具体的应用案例激发学生对该学科的兴趣,鼓励学生能够从人工智能的角度来分析、解决飞行器、机器人所面临的问题和挑战。本课程主要内容包括Python编程基础,机器学习模型,无监督学习、监督学习、深度学习基础知识与实现,并学习如何利用机器学习解决实际问题,从而全面提升自我的《综合能力》。