3 Commits 69b2109d38 ... c841a9339c

Auteur SHA1 Bericht Datum
  15235997262 c841a9339c 20200404307fengjunhao 1 jaar geleden
  15235997262 619c05d937 20200404307fengjunhao 1 jaar geleden
  15235997262 642b5b4cd0 20200404307fengjunhao 1 jaar geleden
100 gewijzigde bestanden met toevoegingen van 635 en 1 verwijderingen
  1. 18 0
      .ipynb_checkpoints/README-checkpoint.md
  2. 3 1
      README.md
  3. 1 0
      aiapp/app.bat
  4. BIN
      aiapp/dev/__pycache__/camera.cpython-39.pyc
  5. 66 0
      aiapp/dev/camera.py
  6. BIN
      aiapp/frame/__pycache__/app.cpython-39.pyc
  7. BIN
      aiapp/frame/__pycache__/win.cpython-39.pyc
  8. 10 0
      aiapp/frame/app.py
  9. 47 0
      aiapp/frame/win.py
  10. 9 0
      aiapp/main.py
  11. BIN
      aiapp/mods/best.pt
  12. BIN
      aiapp/ui/__pycache__/traffic_ui.cpython-39.pyc
  13. 1 0
      aiapp/ui/tools.bat
  14. 64 0
      aiapp/ui/traffic.ui
  15. 47 0
      aiapp/ui/traffic_ui.py
  16. 18 0
      day03/day03/codes/.ipynb_checkpoints/ex_qt01-checkpoint.py
  17. 0 0
      day03/day03/codes/.ipynb_checkpoints/notes-checkpoint.txt
  18. BIN
      day06/infer/00_5.jpg
  19. BIN
      day06/infer/01_0.jpg
  20. BIN
      day06/infer/02_4.jpg
  21. BIN
      day06/infer/__pycache__/model.cpython-39.pyc
  22. 28 0
      day06/infer/ds.py
  23. 38 0
      day06/infer/infer.py
  24. BIN
      day06/infer/lenet5.pt
  25. 75 0
      day06/infer/model.py
  26. BIN
      day06/lenet/00_5.jpg
  27. BIN
      day06/lenet/01_0.jpg
  28. BIN
      day06/lenet/02_4.jpg
  29. BIN
      day06/lenet/03_1.jpg
  30. BIN
      day06/lenet/04_9.jpg
  31. BIN
      day06/lenet/05_2.jpg
  32. BIN
      day06/lenet/06_1.jpg
  33. BIN
      day06/lenet/07_3.jpg
  34. BIN
      day06/lenet/08_1.jpg
  35. BIN
      day06/lenet/09_4.jpg
  36. BIN
      day06/lenet/__pycache__/model.cpython-39.pyc
  37. BIN
      day06/lenet/datasets/MNIST/raw/t10k-images-idx3-ubyte
  38. BIN
      day06/lenet/datasets/MNIST/raw/t10k-images-idx3-ubyte.gz
  39. BIN
      day06/lenet/datasets/MNIST/raw/t10k-labels-idx1-ubyte
  40. BIN
      day06/lenet/datasets/MNIST/raw/t10k-labels-idx1-ubyte.gz
  41. BIN
      day06/lenet/datasets/MNIST/raw/train-images-idx3-ubyte
  42. BIN
      day06/lenet/datasets/MNIST/raw/train-images-idx3-ubyte.gz
  43. BIN
      day06/lenet/datasets/MNIST/raw/train-labels-idx1-ubyte
  44. BIN
      day06/lenet/datasets/MNIST/raw/train-labels-idx1-ubyte.gz
  45. 28 0
      day06/lenet/ds.py
  46. BIN
      day06/lenet/lenet5.pt
  47. 75 0
      day06/lenet/model.py
  48. 107 0
      day06/lenet/train.py
  49. BIN
      day06/train.zip
  50. BIN
      day06/train/train/00000.png
  51. BIN
      day06/train/train/00001.png
  52. BIN
      day06/train/train/00002.png
  53. BIN
      day06/train/train/00003.png
  54. BIN
      day06/train/train/00004.png
  55. BIN
      day06/train/train/00005.png
  56. BIN
      day06/train/train/00006.png
  57. BIN
      day06/train/train/00007.png
  58. BIN
      day06/train/train/00008.png
  59. BIN
      day06/train/train/00009.png
  60. BIN
      day06/train/train/00010.png
  61. BIN
      day06/train/train/00011.png
  62. BIN
      day06/train/train/00012.png
  63. BIN
      day06/train/train/00013.png
  64. BIN
      day06/train/train/00014.png
  65. BIN
      day06/train/train/00015.png
  66. BIN
      day06/train/train/00016.png
  67. BIN
      day06/train/train/00017.png
  68. BIN
      day06/train/train/00018.png
  69. BIN
      day06/train/train/00019.png
  70. BIN
      day06/train/train/00020.png
  71. BIN
      day06/train/train/00021.png
  72. BIN
      day06/train/train/00022.png
  73. BIN
      day06/train/train/00023.png
  74. BIN
      day06/train/train/00024.png
  75. BIN
      day06/train/train/00025.png
  76. BIN
      day06/train/train/00026.png
  77. BIN
      day06/train/train/00027.png
  78. BIN
      day06/train/train/00028.png
  79. BIN
      day06/train/train/00029.png
  80. BIN
      day06/train/train/00030.png
  81. BIN
      day06/train/train/00031.png
  82. BIN
      day06/train/train/00032.png
  83. BIN
      day06/train/train/00033.png
  84. BIN
      day06/train/train/00034.png
  85. BIN
      day06/train/train/00035.png
  86. BIN
      day06/train/train/00036.png
  87. BIN
      day06/train/train/00037.png
  88. BIN
      day06/train/train/00038.png
  89. BIN
      day06/train/train/00039.png
  90. BIN
      day06/train/train/00040.png
  91. BIN
      day06/train/train/00041.png
  92. BIN
      day06/train/train/00042.png
  93. BIN
      day06/train/train/00043.png
  94. BIN
      day06/train/train/00044.png
  95. BIN
      day06/train/train/00045.png
  96. BIN
      day06/train/train/00046.png
  97. BIN
      day06/train/train/00047.png
  98. BIN
      day06/train/train/00048.png
  99. BIN
      day06/train/train/00049.png
  100. BIN
      day06/train/train/00050.png

+ 18 - 0
.ipynb_checkpoints/README-checkpoint.md

@@ -0,0 +1,18 @@
+##day01实训日志
+**任务**
+>
+**内容**
+**要求**
+**提交**
+--------
+##day02实训日志
+*任务**
+>处理图像,及上传日志
+**内容**仓库的建立
+**要求**用仓库绑定文件夹
+**提交**
+--
+##day02天看看行不行##
+
+>dhuadkuhauk    
+**内容真实**

+ 3 - 1
README.md

@@ -12,5 +12,7 @@
 **要求**用仓库绑定文件夹
 **提交**
 --
+##day02天看看行不行##
 
-就离谱嘎嘎真是
+>dhuadkuhauk    
+**内容真实**

+ 1 - 0
aiapp/app.bat

@@ -0,0 +1 @@
+python main.py

BIN
aiapp/dev/__pycache__/camera.cpython-39.pyc


+ 66 - 0
aiapp/dev/camera.py

@@ -0,0 +1,66 @@
+from PyQt5.QtCore import QThread    # 引入多线程,设备是多个,一个设备一个任务
+import cv2
+import numpy as np
+from ultralytics import YOLO   # 1
+# from cv2 import VideoCapture 
+
+# 1. 定义信号(引入)
+from PyQt5.QtCore import pyqtSignal
+
+class CameraDev(QThread):
+    # 定义信号(定义)
+    sig_video = pyqtSignal(bytes, int, int, int)  # 信号传递的数据(图像的二进制数据,字节序列(bytes), 图像高度int,宽度int,通道数int)
+    def __init__(self):
+        super(CameraDev, self).__init__()
+        # 开始视频抓取的任务初始化
+        # 初始化摄像头
+        self.cam = cv2.VideoCapture(
+            0, # 摄像头的编号,从0
+            cv2.CAP_DSHOW # 视频的处理调用DirectX 3D (DirectShow)
+        )
+        self.isOver = False
+        self.model = YOLO("mods/best.pt")   # 2
+
+    def run(self):
+        # kernel = np.array([  # 深度学习就是找到一个kernel是的特征对分类有效
+        #     [0, -2, 0],
+        #     [-2, 8, -2],
+        #     [0, -2, 0]
+        # ])
+        # 设备线程的任务,run结束,则任务结束
+        while not self.isOver:
+            # 反复抓取视频处理
+            # print("设备准备工作!")
+            status, img = self.cam.read()  # 从摄像头读取图像
+            if status:
+                # print(img.shape)
+                # 显示图像
+                # 调用人工智能模块,进行图像识别(处理)
+                # img = cv2.GaussianBlur(img, (3, 3), 2.0)
+                # img = cv2.filter2D(img, -1, kernel, delta=200.0)
+                result = self.model(img)  # 3
+                # 处理结果  # 4
+                boxes = result[0].boxes
+                names = result[0].names
+                if len(boxes) >= 1:
+                    cls = int(boxes.cls[0].cpu().item())
+                    conf = boxes.conf[0].cpu().item()
+                    x1, y1, x2, y2 = boxes.xyxy[0].cpu().numpy().astype(np.int32)
+                    # 标注:目标区域,名字,概率
+                    img = cv2.rectangle(img, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=5)
+                    img = cv2.putText(img, F"{names[cls]}:{conf:.2f}", (x1, y1), 0, 3, color=(255, 0, 0), thickness=3)
+
+                # 2. 发送信号
+                self.sig_video.emit(img.tobytes(), img.shape[0], img.shape[1], img.shape[2])
+            QThread.usleep(100000)  # 1000000微秒 = 1秒
+    
+    def close(self):
+        # 停止多任务
+        self.isOver = True
+        while self.isRunning():
+            pass
+
+        print("线程终止")
+        # 释放设备
+        self.cam.release()
+        print("设备释放")

BIN
aiapp/frame/__pycache__/app.cpython-39.pyc


BIN
aiapp/frame/__pycache__/win.cpython-39.pyc


+ 10 - 0
aiapp/frame/app.py

@@ -0,0 +1,10 @@
+from PyQt5.QtWidgets import QApplication
+from frame.win import Win
+
+class App(QApplication):  # 扩展QApplication类
+    def __init__(self):   # 实现构造器
+        super(App, self).__init__([])   # 调用父类构造器(参数:命令行参数)
+
+        # 调用Win
+        self.win = Win()
+        self.win.show()

+ 47 - 0
aiapp/frame/win.py

@@ -0,0 +1,47 @@
+from PyQt5.QtWidgets import QDialog
+from PyQt5.QtGui import QImage  #,QPxmap
+from PyQt5.QtGui import QPixmap  
+# 引入
+from ui.traffic_ui import Ui_Dialog
+
+from dev.camera import CameraDev
+
+class Win(QDialog):  # 扩展QDialog(新增,覆盖功能)
+    def __init__(self):  # 实现构造器(完成初始化,数据初始化,功能初始化)
+        super(Win, self).__init__()   # 调用父类构造器
+        # 调用ui
+        # 创建对象
+        self.ui = Ui_Dialog()  
+        # 关联ui到当前窗体
+        self.ui.setupUi(self)
+        
+        # 创建一个设备对象
+        self.dev = CameraDev()
+        # 启动设备线程工作
+        self.dev.start()
+
+        # 3. 绑定信号与槽。
+        self.dev.sig_video.connect(self.showVideo)
+
+    # 3. 定义槽(Slot)函数 (Qt技术:信号与槽),一定与信号同型
+    def showVideo(self, data, h, w, c):
+        # print("(",h, ",", w, ",",c, ")")  # python格式字符串
+        # 1. 使用data,h, w, c创建QImage
+        q_img = QImage(
+            data,    # 图像的字节数组
+            w,       # 图像宽度
+            h,       # 图像高度
+            w * c,   # 图像每行字节数
+            QImage.Format_BGR888   # 图像格式BGR,每个通道8个bit,1个字节
+        )
+        # 2. 使用QImage创建QPixmap
+        pix_img = QPixmap.fromImage(q_img)  # 自动从QImage转换为QPixmap,QLabel只支持QPixmap格式
+
+        # 3. 显示QLabel:lblVideo  
+        self.ui.label.setPixmap(pix_img)
+
+        # 4 适当的缩放
+        self.ui.label.setScaledContents(True)
+        def closeEvent(self, e): # 当窗体关闭前会调用
+         self.dev.close()
+         print("窗口关闭")

+ 9 - 0
aiapp/main.py

@@ -0,0 +1,9 @@
+import PyQt5.QtCore
+from frame.app import App
+
+PyQt5.QtCore.QCoreApplication.setAttribute(PyQt5.QtCore.Qt.AA_EnableHighDpiScaling)
+app = App()
+
+app.exec()  # 消息循环(程序循环处理操作系统发过来的消息)阻塞函数
+
+print("程序正常终止")

BIN
aiapp/mods/best.pt


BIN
aiapp/ui/__pycache__/traffic_ui.cpython-39.pyc


+ 1 - 0
aiapp/ui/tools.bat

@@ -0,0 +1 @@
+pyuic5 -o traffic_ui.py traffic.ui

+ 64 - 0
aiapp/ui/traffic.ui

@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>Dialog</class>
+ <widget class="QDialog" name="Dialog">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>1069</width>
+    <height>767</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>智能交通检测系统</string>
+  </property>
+  <widget class="QPushButton" name="pushButton">
+   <property name="geometry">
+    <rect>
+     <x>400</x>
+     <y>660</y>
+     <width>93</width>
+     <height>28</height>
+    </rect>
+   </property>
+   <property name="styleSheet">
+    <string notr="true">border-width:1px;
+border-style: solid;
+border-randius:10px;
+border-top-color:#ffffff;
+border-left-color:#ffffff;
+border-right-color:#888888;
+border-bottom-color:#888888;</string>
+   </property>
+   <property name="text">
+    <string>处理按钮</string>
+   </property>
+  </widget>
+  <widget class="QLabel" name="label">
+   <property name="geometry">
+    <rect>
+     <x>50</x>
+     <y>90</y>
+     <width>651</width>
+     <height>531</height>
+    </rect>
+   </property>
+   <property name="styleSheet">
+    <string notr="true">border-width:3px;
+border-style:solid;
+border-color:blue;
+border-radius:10px;
+border-top-color:red;
+border-bottom-color:green;
+border-left-color:pink;
+border-right-color:black;</string>
+   </property>
+   <property name="text">
+    <string>视频处理区</string>
+   </property>
+  </widget>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>

+ 47 - 0
aiapp/ui/traffic_ui.py

@@ -0,0 +1,47 @@
+# -*- coding: utf-8 -*-
+
+# Form implementation generated from reading ui file 'traffic.ui'
+#
+# Created by: PyQt5 UI code generator 5.15.9
+#
+# WARNING: Any manual changes made to this file will be lost when pyuic5 is
+# run again.  Do not edit this file unless you know what you are doing.
+
+
+from PyQt5 import QtCore, QtGui, QtWidgets
+
+
+class Ui_Dialog(object):
+    def setupUi(self, Dialog):
+        Dialog.setObjectName("Dialog")
+        Dialog.resize(1069, 767)
+        self.pushButton = QtWidgets.QPushButton(Dialog)
+        self.pushButton.setGeometry(QtCore.QRect(400, 660, 93, 28))
+        self.pushButton.setStyleSheet("border-width:1px;\n"
+"border-style: solid;\n"
+"border-randius:10px;\n"
+"border-top-color:#ffffff;\n"
+"border-left-color:#ffffff;\n"
+"border-right-color:#888888;\n"
+"border-bottom-color:#888888;")
+        self.pushButton.setObjectName("pushButton")
+        self.label = QtWidgets.QLabel(Dialog)
+        self.label.setGeometry(QtCore.QRect(21, 44, 651, 531))
+        self.label.setStyleSheet("border-width:3px;\n"
+"border-style:solid;\n"
+"border-color:blue;\n"
+"border-radius:10px;\n"
+"border-top-color:red;\n"
+"border-bottom-color:green;\n"
+"border-left-color:pink;\n"
+"border-right-color:black;")
+        self.label.setObjectName("label")
+
+        self.retranslateUi(Dialog)
+        QtCore.QMetaObject.connectSlotsByName(Dialog)
+
+    def retranslateUi(self, Dialog):
+        _translate = QtCore.QCoreApplication.translate
+        Dialog.setWindowTitle(_translate("Dialog", "智能交通检测系统"))
+        self.pushButton.setText(_translate("Dialog", "处理按钮"))
+        self.label.setText(_translate("Dialog", "视频处理区"))

+ 18 - 0
day03/day03/codes/.ipynb_checkpoints/ex_qt01-checkpoint.py

@@ -0,0 +1,18 @@
+#引入模块
+from PyQt5.QtWidgets import QApplication
+from PyQt5.QtWidgets import QDialog
+from PyQtf5.QtWidgets import QMainWindow
+from PyQtf5.QtWidgets 
+#创建Qt应用
+app = QApplication([])    # 参数:命令行参数
+"""
+  Qt的应用必须在App中间
+"""
+dlg= QDialog()
+dlg.show()
+app.exec()   #让应用程序进入消息循环
+
+
+
+
+

+ 0 - 0
day03/day03/codes/.ipynb_checkpoints/notes-checkpoint.txt


BIN
day06/infer/00_5.jpg


BIN
day06/infer/01_0.jpg


BIN
day06/infer/02_4.jpg


BIN
day06/infer/__pycache__/model.cpython-39.pyc


+ 28 - 0
day06/infer/ds.py

@@ -0,0 +1,28 @@
+from torchvision.datasets import MNIST
+from torchvision.transforms import Compose, ToTensor
+import numpy as np
+import cv2
+
+# 加载数据集(如果不存在则下载)
+transform = Compose([ToTensor()]) # 把图像转换为float的张量
+ds_mnist = MNIST(root="datasets", train=True, download=True, transform=transform)
+
+# print(len(ds_mnist))
+
+# print(ds_mnist[0][0].shape)
+# print(type(ds_mnist))
+# print(ds_mnist[0][1])  # 类别
+
+# print(ds_mnist[0][0])
+
+# 把图像保存为文件
+for i in range(10):  
+    img, cls = ds_mnist[i]   # img是0-1之间float
+
+    img = img.mul(255)  # 0-255
+    img = img.numpy().copy()
+    img = img.astype(np.uint8) # 转为整数
+
+    # 转换成28 * 28 * 1的图像矩阵
+    img = img.transpose(1, 2, 0) # 1 * 28 * 28 -> 28 * 28 * 1
+    cv2.imwrite(F"{i:02d}_{cls}.jpg", img)

+ 38 - 0
day06/infer/infer.py

@@ -0,0 +1,38 @@
+from model import LeNet5
+import torch
+import cv2
+import numpy as np
+
+class  DigitClassifier:
+    def __init__(self): # 初始化
+        super(DigitClassifier, self).__init__()
+        # 创建网络
+        self.net = LeNet5()
+        # 加载模型算子(训练好的模型)
+        state = torch.load("lenet5.pt")
+        self.net.load_state_dict(state)
+
+    
+    def recognize_file(self, digit_file): # 输入图像文件
+        # 1. 读取文件
+        img = cv2.imread(digit_file)
+        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
+        # 2. 处理文件:图像转为NCHW格式的float张量
+        img = img.astype(np.float32) 
+        img = img / 255.0   # 像素转换为0-1之间的值
+        img = torch.from_numpy(img).clone()  # 把矩阵转换为张量
+        img = img.view(1, 1, 28, 28)  # 模型支持4维图像 NCHW
+        # 3. 调用self.net预测
+        y = self.net(img)
+        # 4. 处理预测结果:类别与概率
+        cls = torch.argmax(y, dim=1).item()  # item取张量的值
+        prob = y[0][cls].item()
+        print(cls, prob)
+        return cls,  prob# 返回类别,返回这个类别的概率
+
+
+
+if __name__ == "__main__":
+    classifier = DigitClassifier() # 生成分类器
+    cls, prob = classifier.recognize_file("01_0.jpg")
+    print(F"类别:{cls},概率:{prob}")

BIN
day06/infer/lenet5.pt


+ 75 - 0
day06/infer/model.py

@@ -0,0 +1,75 @@
+from torch.nn import Module    # 扩展该类实现我们自己的深度网络模型
+from torch.nn import Conv2d, Linear   # 卷积运算(特征抽取),全邻接线性运算(分类器)
+from torch.nn.functional import relu, max_pool2d, avg_pool2d  # relu折线函数,maxpool(从数组返回一个最大值)
+import torch
+
+class LeNet5(Module):
+    # 构造器
+    def __init__(self, class_num=10):  # 10手写数字的分类。一共10个类别
+        super(LeNet5, self).__init__()
+        """
+            5层  (28 * 28 * 1)
+                |- 1. 卷积5 * 5 -> (28 * 28 * 6)    -(2, 2) -> (14, 14 , 6)
+                |- 2. 卷积5 * 5 -> (10 * 10 * 16)   -(2, 2) -> (5, 5, 16)
+                |- 3. 卷积5 * 5 -> (1 * 1 * 120)   
+                |- 4. 全连接 120 -> 84 
+                |- 5. 全连接 84 - 10 (1, 0, 0, 0, 0, 0, 0, 0, 0, 0)  取概率最大的下标就是识别出来的数字
+        """
+        self.conv1 = Conv2d(in_channels=1, out_channels=6,  kernel_size=5, stride=1, padding=2)
+        self.conv2 = Conv2d(in_channels=6, out_channels=16, kernel_size=5, stride=1, padding=0)
+        self.conv3 = Conv2d(in_channels=16, out_channels=120, kernel_size=5, stride=1,padding=0)
+
+        self.fc1 = Linear(120, 84)
+        self.fc2 = Linear(84, 10)
+
+
+    # 预测函数
+    def forward(self, x):
+        """
+            x表述输入图像, 格式是:[NHWC]
+        """
+        y = x
+        # 计算预测
+        # 第一层网络
+        y = self.conv1(y)  # (28*28*1) -> (28*28*6)
+        y = max_pool2d(y, (2, 2))  # (28*28*6) -> (14*14*6)
+        y = relu(y)        # 过滤负值(所有的负值设置为0)
+
+        # 第二层
+        y = self.conv2(y)   # (14*14*6) -> (10*10*16)
+        y = max_pool2d(y, (2, 2))   # (10*10*16) -> (5*5*16)
+        y = relu(y)         # 过滤负值
+
+        # 第三层
+        y = self.conv3(y)   # (5*5*16) -> (1*1*120)
+
+        # 把y从(1*1*120) -> (120)向量
+        y = y.view(-1, 120)  
+
+        # 第四层
+        y = self.fc1(y)
+        y = relu(y)
+
+        # 第五层
+        y = self.fc2(y)
+        # y = relu(y)        # 这个激活函数已经没有意义
+
+        # 把向量的分量全部转换0-1之间的值(概率)     
+        y = torch.softmax(y, dim=1) 
+
+        return y
+
+
+# print(__name__)
+if __name__ == "__main__":  # 表示是独立执行册程序块
+    # 下面代码被调用,则执行不到。
+    img = torch.randint(0, 256, (1, 1, 28, 28))  # 构造一个随机矩阵 == 噪音图像[NCHW]
+    img = img.float()    # 神经网络输入的必须是float类型
+    net = LeNet5()
+    y = net(img)
+    # y = net.forward(img)  # 等价于y = net(img)
+
+    # 判定最大下标
+    cls = torch.argmax(y, dim=1)
+    print(F"识别的结果是:{cls.numpy()[0]}")
+    print(y)

BIN
day06/lenet/00_5.jpg


BIN
day06/lenet/01_0.jpg


BIN
day06/lenet/02_4.jpg


BIN
day06/lenet/03_1.jpg


BIN
day06/lenet/04_9.jpg


BIN
day06/lenet/05_2.jpg


BIN
day06/lenet/06_1.jpg


BIN
day06/lenet/07_3.jpg


BIN
day06/lenet/08_1.jpg


BIN
day06/lenet/09_4.jpg


BIN
day06/lenet/__pycache__/model.cpython-39.pyc


BIN
day06/lenet/datasets/MNIST/raw/t10k-images-idx3-ubyte


BIN
day06/lenet/datasets/MNIST/raw/t10k-images-idx3-ubyte.gz


BIN
day06/lenet/datasets/MNIST/raw/t10k-labels-idx1-ubyte


BIN
day06/lenet/datasets/MNIST/raw/t10k-labels-idx1-ubyte.gz


BIN
day06/lenet/datasets/MNIST/raw/train-images-idx3-ubyte


BIN
day06/lenet/datasets/MNIST/raw/train-images-idx3-ubyte.gz


BIN
day06/lenet/datasets/MNIST/raw/train-labels-idx1-ubyte


BIN
day06/lenet/datasets/MNIST/raw/train-labels-idx1-ubyte.gz


+ 28 - 0
day06/lenet/ds.py

@@ -0,0 +1,28 @@
+from torchvision.datasets import MNIST
+from torchvision.transforms import Compose, ToTensor
+import numpy as np
+import cv2
+
+# 加载数据集(如果不存在则下载)
+transform = Compose([ToTensor()]) # 把图像转换为float的张量
+ds_mnist = MNIST(root="datasets", train=True, download=True, transform=transform)
+
+# print(len(ds_mnist))
+
+# print(ds_mnist[0][0].shape)
+# print(type(ds_mnist))
+# print(ds_mnist[0][1])  # 类别
+
+# print(ds_mnist[0][0])
+
+# 把图像保存为文件
+for i in range(10):  
+    img, cls = ds_mnist[i]   # img是0-1之间float
+
+    img = img.mul(255)  # 0-255
+    img = img.numpy().copy()
+    img = img.astype(np.uint8) # 转为整数
+
+    # 转换成28 * 28 * 1的图像矩阵
+    img = img.transpose(1, 2, 0) # 1 * 28 * 28 -> 28 * 28 * 1
+    cv2.imwrite(F"{i:02d}_{cls}.jpg", img)

BIN
day06/lenet/lenet5.pt


+ 75 - 0
day06/lenet/model.py

@@ -0,0 +1,75 @@
+from torch.nn import Module    # 扩展该类实现我们自己的深度网络模型
+from torch.nn import Conv2d, Linear   # 卷积运算(特征抽取),全邻接线性运算(分类器)
+from torch.nn.functional import relu, max_pool2d, avg_pool2d  # relu折线函数,maxpool(从数组返回一个最大值)
+import torch
+
+class LeNet5(Module):
+    # 构造器
+    def __init__(self, class_num=10):  # 10手写数字的分类。一共10个类别
+        super(LeNet5, self).__init__()
+        """
+            5层  (28 * 28 * 1)
+                |- 1. 卷积5 * 5 -> (28 * 28 * 6)    -(2, 2) -> (14, 14 , 6)
+                |- 2. 卷积5 * 5 -> (10 * 10 * 16)   -(2, 2) -> (5, 5, 16)
+                |- 3. 卷积5 * 5 -> (1 * 1 * 120)   
+                |- 4. 全连接 120 -> 84 
+                |- 5. 全连接 84 - 10 (1, 0, 0, 0, 0, 0, 0, 0, 0, 0)  取概率最大的下标就是识别出来的数字
+        """
+        self.conv1 = Conv2d(in_channels=1, out_channels=6,  kernel_size=5, stride=1, padding=2)
+        self.conv2 = Conv2d(in_channels=6, out_channels=16, kernel_size=5, stride=1, padding=0)
+        self.conv3 = Conv2d(in_channels=16, out_channels=120, kernel_size=5, stride=1,padding=0)
+
+        self.fc1 = Linear(120, 84)
+        self.fc2 = Linear(84, 10)
+
+
+    # 预测函数
+    def forward(self, x):
+        """
+            x表述输入图像, 格式是:[NHWC]
+        """
+        y = x
+        # 计算预测
+        # 第一层网络
+        y = self.conv1(y)  # (28*28*1) -> (28*28*6)
+        y = max_pool2d(y, (2, 2))  # (28*28*6) -> (14*14*6)
+        y = relu(y)        # 过滤负值(所有的负值设置为0)
+
+        # 第二层
+        y = self.conv2(y)   # (14*14*6) -> (10*10*16)
+        y = max_pool2d(y, (2, 2))   # (10*10*16) -> (5*5*16)
+        y = relu(y)         # 过滤负值
+
+        # 第三层
+        y = self.conv3(y)   # (5*5*16) -> (1*1*120)
+
+        # 把y从(1*1*120) -> (120)向量
+        y = y.view(-1, 120)  
+
+        # 第四层
+        y = self.fc1(y)
+        y = relu(y)
+
+        # 第五层
+        y = self.fc2(y)
+        # y = relu(y)        # 这个激活函数已经没有意义
+
+        # 把向量的分量全部转换0-1之间的值(概率)     
+        y = torch.softmax(y, dim=1) 
+
+        return y
+
+
+# print(__name__)
+if __name__ == "__main__":  # 表示是独立执行册程序块
+    # 下面代码被调用,则执行不到。
+    img = torch.randint(0, 256, (1, 1, 28, 28))  # 构造一个随机矩阵 == 噪音图像[NCHW]
+    img = img.float()    # 神经网络输入的必须是float类型
+    net = LeNet5()
+    y = net(img)
+    # y = net.forward(img)  # 等价于y = net(img)
+
+    # 判定最大下标
+    cls = torch.argmax(y, dim=1)
+    print(F"识别的结果是:{cls.numpy()[0]}")
+    print(y)

+ 107 - 0
day06/lenet/train.py

@@ -0,0 +1,107 @@
+from model import LeNet5    # 引入我们写好的神经网络(也是我们要训练的网络)
+from torch.nn import CrossEntropyLoss  # 引入损失函数(用来做分类损失最佳)
+from torch.optim import Adam  # 损失函数优化器(其他损失函数的最小值:使用梯度下降法)
+from torchvision.datasets import MNIST # 数据集
+from torchvision.transforms import Compose, ToTensor  # 图像转换为张量
+from torch.utils.data import DataLoader # 批次数据集(数据集不是一次训练,而是分成多个批次)
+import torch  # 调用torch的基本函数
+import os   # 路径与文件处理
+
+class Trainer:
+    def __init__(self):
+        # 判定电脑安装GPU的环境:cuda,cnn,pytorch-
+        self.CUDA = False # torch.cuda.is_available()   # 返回逻辑值:True:支持GPU,False不支持GPU
+        # 准备训练需要的数据、损失函数,优化器,学习率
+        self.lr = 0.0001
+        self.m_file = "lenet5.pt"  # 模型的保存文件
+        self.net = LeNet5() # 需要训练的网络
+        if self.CUDA:
+            self.net.cuda()   # 把net网络模型存储到GPU上面
+
+        # 加载上一次训练的模型
+        if os.path.exists(self.m_file):
+            # 存在就加载
+            print("加载模型中...")
+            state = torch.load(self.m_file)  # 读取文件
+            self.net.load_state_dict(state)
+        else:
+            print("模型存在,重头训练")    
+        self.loss_f = CrossEntropyLoss()  # 损失函数
+        self.optimizer = Adam(self.net.parameters(), lr=self.lr)  # 优化器
+
+
+        # 数据集 - 训练集
+        self.trans = Compose([ToTensor()])
+        self.ds_train = MNIST(root="datasets", download=True, train=True, transform=self.trans)
+        self.bt_train = DataLoader(self.ds_train, batch_size=1000, shuffle=True) 
+
+        # 数据集 - 验证集
+        self.ds_valid = MNIST(root="datasets", download=True, train=False, transform=self.trans)
+        self.bt_valid = DataLoader(self.ds_valid, batch_size=1000, shuffle=False) #  shuffle=False是否随机打乱
+
+
+
+    def train_one(self):
+        # 训练一轮epoch,60批次batch,每个批次1000张录像
+        # 循环训练每个批次
+        batch = 1
+        for x, y in self.bt_train:
+            # print(F"\t|-第{batch:02d}批次训练。")
+            if self.CUDA:
+                x = x.cuda()
+                y = y.cuda()
+            y_ = self.net(x)  # 进行预测
+            # 计算误差
+            loss = self.loss_f(y_, y)   # 使用真实的标签与预测标签计算sunshi
+            # 优化卷积核与全链接矩阵
+            self.optimizer.zero_grad()
+            loss.backward()   # 使用导数计算梯度
+            self.optimizer.step()  # 更新梯度,优化网络模型
+            batch+=1
+
+    @torch.no_grad()   # 该函数中的运算都不会进行求导跟踪
+    def valid(self):
+        # 使用测试数据集验证 (准确率,损失值)
+        all_num = 0.0 # 验证的样本数
+        acc_num = 0.0 # 识别正确数量
+        all_loss = 0.0 # 累加每个样本的损失
+        for t_x, t_y in self.bt_valid:
+            if self.CUDA:
+                t_x = t_x.cuda()
+                t_y = t_y.cuda()
+            # 统计样本数
+            all_num  += len(t_x)
+            #  预测
+            t_y_ = self.net(t_x)
+            # 判定准确
+            y_cls = torch.argmax(t_y_, dim=1)
+            # 统计正确率
+            acc_num  += (y_cls == t_y).float().sum()
+            # 统计损失
+            all_loss += self.loss_f(t_y_, t_y)
+        print(F"\t|- 训练损失:{all_loss/all_num:8.6f}")
+        print(F"\t|- 准确率:{acc_num * 100.0/all_num:5.2f}%")
+
+
+
+    def train(self, epoch):
+        # 训练指定的论数
+        for e in range(epoch):
+            print(F"第{e:04d}轮训练。")
+            self.train_one()
+            self.valid()
+            # 保存训练的模型
+            torch.save(self.net.state_dict(), self.m_file)
+
+if __name__ == "__main__":
+    trainer = Trainer()
+    trainer.train(50) # 训练10轮
+    # print(torch.cuda.is_available()) 
+
+"""
+    244K = 6 * 5 * 5 * 8 矩阵  
+          16 * 5 * 5 * 8 
+         120 * 5 * 5 * 8
+             120 * 84* 8
+              84 * 10*8
+"""

BIN
day06/train.zip


BIN
day06/train/train/00000.png


BIN
day06/train/train/00001.png


BIN
day06/train/train/00002.png


BIN
day06/train/train/00003.png


BIN
day06/train/train/00004.png


BIN
day06/train/train/00005.png


BIN
day06/train/train/00006.png


BIN
day06/train/train/00007.png


BIN
day06/train/train/00008.png


BIN
day06/train/train/00009.png


BIN
day06/train/train/00010.png


BIN
day06/train/train/00011.png


BIN
day06/train/train/00012.png


BIN
day06/train/train/00013.png


BIN
day06/train/train/00014.png


BIN
day06/train/train/00015.png


BIN
day06/train/train/00016.png


BIN
day06/train/train/00017.png


BIN
day06/train/train/00018.png


BIN
day06/train/train/00019.png


BIN
day06/train/train/00020.png


BIN
day06/train/train/00021.png


BIN
day06/train/train/00022.png


BIN
day06/train/train/00023.png


BIN
day06/train/train/00024.png


BIN
day06/train/train/00025.png


BIN
day06/train/train/00026.png


BIN
day06/train/train/00027.png


BIN
day06/train/train/00028.png


BIN
day06/train/train/00029.png


BIN
day06/train/train/00030.png


BIN
day06/train/train/00031.png


BIN
day06/train/train/00032.png


BIN
day06/train/train/00033.png


BIN
day06/train/train/00034.png


BIN
day06/train/train/00035.png


BIN
day06/train/train/00036.png


BIN
day06/train/train/00037.png


BIN
day06/train/train/00038.png


BIN
day06/train/train/00039.png


BIN
day06/train/train/00040.png


BIN
day06/train/train/00041.png


BIN
day06/train/train/00042.png


BIN
day06/train/train/00043.png


BIN
day06/train/train/00044.png


BIN
day06/train/train/00045.png


BIN
day06/train/train/00046.png


BIN
day06/train/train/00047.png


BIN
day06/train/train/00048.png


BIN
day06/train/train/00049.png


BIN
day06/train/train/00050.png


Some files were not shown because too many files changed in this diff