├── DeepLearning-Lab ├── README.md ├── lab1 │ ├── MLP.py │ ├── README.md │ ├── ~$实验1 - 深度学习框架熟悉.pptx │ └── 实验1 - 深度学习框架熟悉.pptx ├── lab2 │ ├── AlexNet.py │ ├── README.md │ ├── Result │ │ └── events.out.tfevents.1561000326.DESKTOP-T3AN8OM │ ├── model.pkl │ └── 实验2 - 卷积神经网络实现.pptx ├── lab3 │ ├── README.md │ ├── dataset.py │ ├── logs_resNet │ │ └── events.out.tfevents.1558866099.LAPTOP-BFCS88S3 │ ├── logs_vgg │ │ └── events.out.tfevents.1559188515.LAPTOP-BFCS88S3 │ ├── main.py │ ├── resNet.py │ ├── vgg.py │ ├── 实验3 - VGG&ResNet实现与对比.pptx │ └── 实验报告.pdf ├── lab4 │ ├── 1160300426-李国建-RNN实验报告.pdf │ ├── data_processing.py │ ├── emotion_analysis.py │ ├── glove.6B.50d.txt.7z │ ├── sine.py │ ├── sine_wave_generation.py │ ├── traindata.pt │ └── 实验4 - 循环神经网络.pptx └── lab5 │ ├── 1160300426-李国建-GAN实验报告.pdf │ ├── draw.py │ ├── gan.py │ ├── points.mat │ ├── result │ ├── gan │ │ ├── epoch10.png │ │ ├── epoch100.png │ │ ├── epoch15.png │ │ ├── epoch20.png │ │ ├── epoch25.png │ │ ├── epoch30.png │ │ ├── epoch35.png │ │ ├── epoch40.png │ │ ├── epoch45.png │ │ ├── epoch5.png │ │ ├── epoch50.png │ │ ├── epoch55.png │ │ ├── epoch60.png │ │ ├── epoch65.png │ │ ├── epoch70.png │ │ ├── epoch75.png │ │ ├── epoch80.png │ │ ├── epoch85.png │ │ ├── epoch90.png │ │ ├── epoch95.png │ │ └── final.gif │ ├── wgan │ │ ├── epoch10.png │ │ ├── epoch100.png │ │ ├── epoch15.png │ │ ├── epoch20.png │ │ ├── epoch25.png │ │ ├── epoch30.png │ │ ├── epoch35.png │ │ ├── epoch40.png │ │ ├── epoch45.png │ │ ├── epoch5.png │ │ ├── epoch50.png │ │ ├── epoch55.png │ │ ├── epoch60.png │ │ ├── epoch65.png │ │ ├── epoch70.png │ │ ├── epoch75.png │ │ ├── epoch80.png │ │ ├── epoch85.png │ │ ├── epoch90.png │ │ ├── epoch95.png │ │ └── final.gif │ └── wgan_gp │ │ ├── epoch10.png │ │ ├── epoch100.png │ │ ├── epoch15.png │ │ ├── epoch20.png │ │ ├── epoch25.png │ │ ├── epoch30.png │ │ ├── epoch35.png │ │ ├── epoch40.png │ │ ├── epoch45.png │ │ ├── epoch5.png │ │ ├── epoch50.png │ │ ├── epoch55.png │ │ ├── epoch60.png │ │ ├── epoch65.png │ │ ├── epoch70.png │ │ ├── epoch75.png │ │ ├── epoch80.png │ │ ├── epoch85.png │ │ ├── epoch90.png │ │ ├── epoch95.png │ │ └── final.gif │ ├── wgan.py │ ├── wgan_gp.py │ └── 实验5 - 生成式对抗网络.pdf ├── Pattern-Recognition-Lab ├── GMM │ ├── featureextraction.py │ ├── modeltraining.py │ ├── test.py │ ├── testSamplePath.txt │ └── trainingDataPath.txt ├── lab1-pca.py ├── lab1-svm.py └── 实验报告-1160300426-李国建.pdf ├── README.md └── lecture ├── DeepLearning ├── 13. 多层感知器.pdf ├── 14. 多层感知器-2.pdf ├── 15. 卷积神经网络-1.pdf ├── 16. 卷积神经网络-2.pdf ├── 17. 时序神经网络-1.pdf ├── 18. 时序神经网络-2.pdf ├── 19. 生成式对抗网络.pdf ├── 20. 图像增强与复原.pdf └── 21.目标检测与分割0614v2.pdf ├── PatternRecognition ├── 2019-1.pdf ├── 2019-2.pdf ├── 2019-3线性分类器与支持向量机.pdf ├── 2019-4讲特征选择与特征提取.pdf ├── 2019-5 统计分类器及其学习I.pdf ├── 2019-6 统计分类器及其学习II.pdf ├── 2019-7 聚类分析.pdf ├── 矩阵分析简介.pdf └── 矩阵的微分.pdf └── ~$2019-1.pptx /DeepLearning-Lab/README.md: -------------------------------------------------------------------------------- 1 | # 介绍 2 | 3 | ## 实验一(MLP) 4 | 5 | * 使用PyTorch实现MLP,并在MNIST数据集上验证 6 | 7 | 没什么好说的,深度学习的入门项目,体验一下就好。 8 | 9 | 据助教说,这次实验不算成绩,也不知道最后算没算。不过这都无所谓了,重在学习,分数次要(哈哈,千万别当真,分数还是要的) 10 | 11 | ## 实验二(AlexNet) 12 | 13 | * 基于 PyTorch 实现 AlexNet 14 | * 在 Cifar-10 数据集上进行验证 15 | * 使用tensorboard进行训练数据可视化(Loss 曲线) 16 | * 如有条件,尝试不同参数的影响,尝试其他网络结构 17 | * 请勿使用torchvision.models.AlexNet 18 | 19 | **注: 更改后的 AlexNet,但结构相似。**主要目的还是为了体验一下卷积神经网络,因此为了不使用太大的数据集,要更改一下参数 20 | 21 | ## 实验三(VGG/ResNet) 22 | 23 | * 基于PyTorch实现 VGG/ResNet 结构,并在 Cifar-10 数据集上进行验证 24 | 1. VGG要求实现VGG-11(Conv部分按论文实现,Classifier直接一层全连接即可) 25 | 2. ResNet要求实现ResNet-18。均要求5个epoch以内达到50%的测试集精度。 26 | 27 | * 基于 VGG 进行训练方式对比(LR 和优化器的对比) 28 | 1. LR对比三组及以上【此时选用任一种优化器】 29 | 2. 优化器对比 SGD 与 Adam【选用LR对比时的最佳LR】 30 | 31 | * 基于 CUDA 实现,并设置 argparse 参数 32 | 可设定是否使用GPU,通过 argparse 包实现,默认参数设定为GPU 33 | 34 | * 自定义实现 dataset 35 | 基于opencv或Pillow 36 | 37 | ## 实验四(RNN) 38 | 39 | * 对Sine函数进行预测 40 | 给定部分序列的值,进行后续序列值的预测 41 | 42 | * 基于影评数据进行文本情感预测 43 | 基于影评文本,进行文本情感分析 44 | 45 | 实验分为两部分,分别使用了 LSTM 和 GRU: 46 | 47 | 1. 第一部分实现 sine 函数预测,这是 Pytorch 官网的一个例子:[sine 例子](https://github.com/pytorch/examples/blob/master/time_sequence_prediction/generate_sine_wave.py) 48 | 49 | 参考博客:[Pytorch LSTM 时间序列预测](https://blog.csdn.net/duan_zhihua/article/details/84491672) 50 | 51 | 2. 第二部分实现文本情感分析,网络结构参考:[使用 PyTorch RNN 进行文本分类](https://www.jianshu.com/p/46d9dec06199) 52 | 53 | 这部分做的时间很匆忙,我觉得可改进的空间很大。 54 | 55 | ## 实验五(GAN) 56 | 57 | * 基于PyTorch实现生成对抗网络 58 | 1. 拟合给定分布 59 | 2. 要求可视化训练过程 60 | 61 | * 对比GAN、WGAN、WGAN-GP(稳定性、性能) 62 | 63 | * 对比不同优化器的影响 64 | 65 | 这次实验算是我做的最顺利的一次实验了,没有出什么 bug,实验效果很好,很神奇。GAN 初体验,感觉很强大 66 | 67 | ## 实验六(自选项目,组队完成,暂时没有放到仓库里) 68 | 69 | ### 基于PyTorch完成一个项目 70 | 71 | 选择范围: 72 | 73 | * 去噪(Denoising) 74 | * 超分辨率(Super-resolution) 75 | * 检测(Detection) 76 | * 分割(Segmentation) 77 | 78 | 本来想打算从后两个高层视觉中选,后来发现太难了,,,去噪的题目上学期图像处理已经做过了(虽然不是神经网),于是选择了超分辨率。 79 | 80 | 使用的是 SRGAN,参考:[PyTorch-SRGAN](https://github.com/aitorzip/PyTorch-SRGAN) 81 | 82 | # 最后 83 | 84 | 感谢左老师的授课还有助教的指导 85 | -------------------------------------------------------------------------------- /DeepLearning-Lab/lab1/MLP.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 系统环境: Windows10 3 | Python版本: 3.7 4 | PyTorch版本: 1.1.0 5 | cuda: no 6 | ''' 7 | import torch 8 | import torch.nn.functional as F # 激励函数的库 9 | from torchvision import datasets 10 | import torchvision.transforms as transforms 11 | import numpy as np 12 | 13 | # 定义全局变量 14 | n_epochs = 10 # epoch 的数目 15 | batch_size = 20 # 决定每次读取多少图片 16 | 17 | # 定义训练集个测试集,如果找不到数据,就下载 18 | train_data = datasets.MNIST(root = './data', train = True, download = True, transform = transforms.ToTensor()) 19 | test_data = datasets.MNIST(root = './data', train = True, download = True, transform = transforms.ToTensor()) 20 | # 创建加载器 21 | train_loader = torch.utils.data.DataLoader(train_data, batch_size = batch_size, num_workers = 0) 22 | test_loader = torch.utils.data.DataLoader(test_data, batch_size = batch_size, num_workers = 0) 23 | 24 | 25 | # 建立一个四层感知机网络 26 | class MLP(torch.nn.Module): # 继承 torch 的 Module 27 | def __init__(self): 28 | super(MLP,self).__init__() # 29 | # 初始化三层神经网络 两个全连接的隐藏层,一个输出层 30 | self.fc1 = torch.nn.Linear(784,512) # 第一个隐含层 31 | self.fc2 = torch.nn.Linear(512,128) # 第二个隐含层 32 | self.fc3 = torch.nn.Linear(128,10) # 输出层 33 | 34 | def forward(self,din): 35 | # 前向传播, 输入值:din, 返回值 dout 36 | din = din.view(-1,28*28) # 将一个多行的Tensor,拼接成一行 37 | dout = F.relu(self.fc1(din)) # 使用 relu 激活函数 38 | dout = F.relu(self.fc2(dout)) 39 | dout = F.softmax(self.fc3(dout), dim=1) # 输出层使用 softmax 激活函数 40 | # 10个数字实际上是10个类别,输出是概率分布,最后选取概率最大的作为预测值输出 41 | return dout 42 | 43 | # 训练神经网络 44 | def train(): 45 | #定义损失函数和优化器 46 | lossfunc = torch.nn.CrossEntropyLoss() 47 | optimizer = torch.optim.SGD(params = model.parameters(), lr = 0.01) 48 | # 开始训练 49 | for epoch in range(n_epochs): 50 | train_loss = 0.0 51 | for data,target in train_loader: 52 | optimizer.zero_grad() # 清空上一步的残余更新参数值 53 | output = model(data) # 得到预测值 54 | loss = lossfunc(output,target) # 计算两者的误差 55 | loss.backward() # 误差反向传播, 计算参数更新值 56 | optimizer.step() # 将参数更新值施加到 net 的 parameters 上 57 | train_loss += loss.item()*data.size(0) 58 | train_loss = train_loss / len(train_loader.dataset) 59 | print('Epoch: {} \tTraining Loss: {:.6f}'.format(epoch + 1, train_loss)) 60 | # 每遍历一遍数据集,测试一下准确率 61 | test() 62 | 63 | # 在数据集上测试神经网络 64 | def test(): 65 | correct = 0 66 | total = 0 67 | with torch.no_grad(): # 训练集中不需要反向传播 68 | for data in test_loader: 69 | images, labels = data 70 | outputs = model(images) 71 | _, predicted = torch.max(outputs.data, 1) 72 | total += labels.size(0) 73 | correct += (predicted == labels).sum().item() 74 | print('Accuracy of the network on the test images: %d %%' % ( 75 | 100 * correct / total)) 76 | return 100.0 * correct / total 77 | 78 | # 声明感知器网络 79 | model = MLP() 80 | 81 | if __name__ == '__main__': 82 | train() 83 | -------------------------------------------------------------------------------- /DeepLearning-Lab/lab1/README.md: -------------------------------------------------------------------------------- 1 | # 实验内容 2 | 3 | 使用PyTorch实现MLP,并在MNIST数据集上验证 4 | 5 | # 介绍 6 | 7 | 这是深度学习课程的第一个实验,主要目的就是熟悉 Pytorch 框架。MLP 是多层感知器,我这次实现的是四层感知器,个人认为,感知器的代码大同小异,尤其是用 Pytorch 实现,除了层数和参数外,代码都很相似。 8 | 9 | Pytorch 写神经网络的主要步骤主要有以下几步: 10 | 11 | 1. **构建网络结构** 12 | 2. **加载数据集** 13 | 3. **训练神经网络(包括优化器的选择和 Loss 的计算)** 14 | 4. **测试神经网络** 15 | 16 | # 过程 17 | ### 构建网络结构 18 | 19 | 神经网络最重要的就是搭建网络,第一步就是定义网络结构。我这里是创建了一个四层的感知器,参数是根据 MNIST 数据集设定的,网络结构如下: 20 | 21 | 22 | # 建立一个四层感知机网络 23 | class MLP(torch.nn.Module): # 继承 torch 的 Module 24 | def __init__(self): 25 | super(MLP,self).__init__() # 26 | # 初始化三层神经网络 两个全连接的隐藏层,一个输出层 27 | self.fc1 = torch.nn.Linear(784,512) # 第一个隐含层 28 | self.fc2 = torch.nn.Linear(512,128) # 第二个隐含层 29 | self.fc3 = torch.nn.Linear(128,10) # 输出层 30 | 31 | def forward(self,din): 32 | # 前向传播, 输入值:din, 返回值 dout 33 | din = din.view(-1,28*28) # 将一个多行的Tensor,拼接成一行 34 | dout = F.relu(self.fc1(din)) # 使用 relu 激活函数 35 | dout = F.relu(self.fc2(dout)) 36 | dout = F.softmax(self.fc3(dout), dim=1) # 输出层使用 softmax 激活函数 37 | # 10个数字实际上是10个类别,输出是概率分布,最后选取概率最大的作为预测值输出 38 | return dout 39 | 40 | 网络结构其实很简单,设置了三层 Linear。隐含层激活函数使用 Relu; 输出层使用 Softmax。网上还有其他的结构使用了 droupout,我觉得入门的话有点高级,而且放在这里并没有什么用,搞得很麻烦还不能提高准确率。 41 | 42 | ### 加载数据集 43 | 44 | 第二步就是定义全局变量,并加载 MNIST 数据集: 45 | 46 | # 定义全局变量 47 | n_epochs = 10 # epoch 的数目 48 | batch_size = 20 # 决定每次读取多少图片 49 | 50 | # 定义训练集个测试集,如果找不到数据,就下载 51 | train_data = datasets.MNIST(root = './data', train = True, download = True, transform = transforms.ToTensor()) 52 | test_data = datasets.MNIST(root = './data', train = True, download = True, transform = transforms.ToTensor()) 53 | # 创建加载器 54 | train_loader = torch.utils.data.DataLoader(train_data, batch_size = batch_size, num_workers = 0) 55 | test_loader = torch.utils.data.DataLoader(test_data, batch_size = batch_size, num_workers = 0) 56 | 57 | 58 | 这里参数很多,所以就有很多需要注意的地方了: 59 | 60 | * **root** 参数的文件夹即使不存在也没关系,会自动创建 61 | 62 | * **transform** 参数,如果不知道要对数据集进行什么变化,这里可自动忽略 63 | 64 | * **batch_size** 参数的大小决定了一次训练多少数据,相当于定义了每个 epoch 中反向传播的次数 65 | 66 | * **num_workers** 参数默认是 0,即不并行处理数据;我这里设置大于 0 的时候,总是报错,建议设成默认值 67 | 68 | 如果不理解 epoch 和 batch_size,可以上网查一下资料。(我刚开始学深度学习的时候也是不懂的) 69 | 70 | ### 训练神经网络 71 | 72 | 第三步就是训练网络了,代码如下: 73 | 74 | # 训练神经网络 75 | def train(): 76 | # 定义损失函数和优化器 77 | lossfunc = torch.nn.CrossEntropyLoss() 78 | optimizer = torch.optim.SGD(params = model.parameters(), lr = 0.01) 79 | # 开始训练 80 | for epoch in range(n_epochs): 81 | train_loss = 0.0 82 | for data,target in train_loader: 83 | optimizer.zero_grad() # 清空上一步的残余更新参数值 84 | output = model(data) # 得到预测值 85 | loss = lossfunc(output,target) # 计算两者的误差 86 | loss.backward() # 误差反向传播, 计算参数更新值 87 | optimizer.step() # 将参数更新值施加到 net 的 parameters 上 88 | train_loss += loss.item()*data.size(0) 89 | train_loss = train_loss / len(train_loader.dataset) 90 | print('Epoch: {} \tTraining Loss: {:.6f}'.format(epoch + 1, train_loss)) 91 | 92 | 训练之前要定义损失函数和优化器,这里其实有很多学问,但本文就不讲了,理论太多了。 93 | 94 | 训练过程就是两层 for 循环:**外层是遍历训练集的次数;内层是每次的批次(batch)**。最后,输出每个 epoch 的 loss。(每次训练的目的是使 loss 函数减小,以达到训练集上更高的准确率) 95 | 96 | ### 测试神经网络 97 | 98 | 最后,就是在测试集上进行测试,代码如下: 99 | 100 | # 在数据集上测试神经网络 101 | def test(): 102 | correct = 0 103 | total = 0 104 | with torch.no_grad(): # 训练集中不需要反向传播 105 | for data in test_loader: 106 | images, labels = data 107 | outputs = model(images) 108 | _, predicted = torch.max(outputs.data, 1) 109 | total += labels.size(0) 110 | correct += (predicted == labels).sum().item() 111 | print('Accuracy of the network on the test images: %d %%' % ( 112 | 100 * correct / total)) 113 | return 100.0 * correct / total 114 | 115 | 这个测试的代码是同学给我的,我觉得这个测试的代码特别好,很简洁,一直用的这个。 116 | 117 | 代码首先设置 **torch.no_grad()**,定义后面的代码不需要计算梯度,能够节省一些内存空间。然后,对测试集中的每个 batch 进行测试,统计总数和准确数,最后计算准确率并输出。 118 | 119 | 通常是选择边训练边测试的,这里先就按步骤一步一步来做。 120 | 121 | 有的测试代码前面要加上 **model.eval()**,表示这是训练状态。但这里不需要,**如果没有 Batch Normalization 和 Dropout 方法,加和不加的效果是一样的**。 122 | 123 | # 效果 124 | 125 | 10 个 epoch 的训练效果,最后能达到大约 85% 的准确率。可以适当增加 epoch,但代码里没有用 gpu 运行,可能会比较慢。 126 | 127 | # 参考 128 | 写代码的时候,很大程度上参考了下面一些文章,感谢各位作者 129 | 130 | 1. [基于Pytorch的MLP实现](https://www.jianshu.com/p/65aed5b33cf2) 131 | 2. [莫烦 Python ——区分类型 (分类)](https://morvanzhou.github.io/tutorials/machine-learning/torch/3-02-classification/) 132 | 3. [使用Pytorch构建MLP模型实现MNIST手写数字识别](https://blog.csdn.net/l1606468155/article/details/89818546) -------------------------------------------------------------------------------- /DeepLearning-Lab/lab1/~$实验1 - 深度学习框架熟悉.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab1/~$实验1 - 深度学习框架熟悉.pptx -------------------------------------------------------------------------------- /DeepLearning-Lab/lab1/实验1 - 深度学习框架熟悉.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab1/实验1 - 深度学习框架熟悉.pptx -------------------------------------------------------------------------------- /DeepLearning-Lab/lab2/AlexNet.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | import torchvision 6 | import torchvision.transforms as transforms 7 | import torch.optim as optim 8 | from tensorboardX import SummaryWriter 9 | 10 | # 定义全局变量 11 | modelPath = './model.pkl' 12 | batchSize = 5 13 | nEpochs = 20 14 | numPrint = 1000 15 | # 定义Summary_Writer 16 | writer = SummaryWriter('./Result') # 数据存放在这个文件夹 17 | # cuda 18 | device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") 19 | 20 | # 加载数据集 (训练集和测试集) 21 | trainset = torchvision.datasets.CIFAR10(root='./Cifar-10', train=True, download=True, transform=transforms.ToTensor()) 22 | trainloader = torch.utils.data.DataLoader(trainset, batch_size=batchSize, shuffle=True) 23 | testset = torchvision.datasets.CIFAR10(root='./Cifar-10', train=False, download=True, transform=transforms.ToTensor()) 24 | testloader = torch.utils.data.DataLoader(testset, batch_size=batchSize, shuffle=False) 25 | 26 | # 定义神经网络 27 | class Net(nn.Module): # 训练 ALexNet 28 | ''' 29 | 三层卷积,三层全连接 (应该是5层卷积,由于图片是 32 * 32,且为了效率,这里设成了 3 层) 30 | ''' 31 | def __init__(self): 32 | super(Net, self).__init__() 33 | # 五个卷积层 34 | self.conv1 = nn.Sequential( # 输入 32 * 32 * 3 35 | nn.Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=1), # (32-3+2)/1+1 = 32 36 | nn.ReLU(), 37 | nn.MaxPool2d(kernel_size=2, stride=2, padding=0) # (32-2)/2+1 = 16 38 | ) 39 | self.conv2 = nn.Sequential( # 输入 16 * 16 * 6 40 | nn.Conv2d(in_channels=6, out_channels=16, kernel_size=3, stride=1, padding=1), # (16-3+2)/1+1 = 16 41 | nn.ReLU(), 42 | nn.MaxPool2d(kernel_size=2, stride=2, padding=0) # (16-2)/2+1 = 8 43 | ) 44 | self.conv3 = nn.Sequential( # 输入 8 * 8 * 16 45 | nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding=1), # (8-3+2)/1+1 = 8 46 | nn.ReLU(), 47 | nn.MaxPool2d(kernel_size=2, stride=2, padding=0) # (8-2)/2+1 = 4 48 | ) 49 | self.conv4 = nn.Sequential( # 输入 4 * 4 * 64 50 | nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1), # (4-3+2)/1+1 = 4 51 | nn.ReLU(), 52 | nn.MaxPool2d(kernel_size=2, stride=2, padding=0) # (4-2)/2+1 = 2 53 | ) 54 | self.conv5 = nn.Sequential( # 输入 2 * 2 * 128 55 | nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1),# (2-3+2)/1+1 = 2 56 | nn.ReLU(), 57 | nn.MaxPool2d(kernel_size=2, stride=2, padding=0) # (2-2)/2+1 = 1 58 | ) # 最后一层卷积层,输出 1 * 1 * 128 59 | # 全连接层 60 | self.dense = nn.Sequential( 61 | nn.Linear(128, 120), 62 | nn.ReLU(), 63 | nn.Linear(120, 84), 64 | nn.ReLU(), 65 | nn.Linear(84, 10) 66 | ) 67 | 68 | def forward(self, x): 69 | x = self.conv1(x) 70 | x = self.conv2(x) 71 | x = self.conv3(x) 72 | x = self.conv4(x) 73 | x = self.conv5(x) 74 | x = x.view(-1, 128) 75 | x = self.dense(x) 76 | return x 77 | 78 | net = Net().to(device) 79 | 80 | # 使用测试数据测试网络 81 | def Accuracy(): 82 | correct = 0 83 | total = 0 84 | with torch.no_grad(): # 训练集中不需要反向传播 85 | for data in testloader: 86 | images, labels = data 87 | images, labels = images.to(device), labels.to(device) # 将输入和目标在每一步都送入GPU 88 | outputs = net(images) 89 | _, predicted = torch.max(outputs.data, 1) # 返回每一行中最大值的那个元素,且返回其索引 90 | total += labels.size(0) 91 | correct += (predicted == labels).sum().item() 92 | print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total)) 93 | return 100.0 * correct / total 94 | 95 | # 训练函数 96 | def train(): 97 | # 定义损失函数和优化器 98 | criterion = nn.CrossEntropyLoss() # 交叉熵损失 99 | optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) # 随机梯度下降 100 | iter = 0 101 | num = 1 102 | # 训练网络 103 | for epoch in range(nEpochs): # loop over the dataset multiple times 104 | running_loss = 0.0 105 | for i, data in enumerate(trainloader, 0): 106 | iter = iter + 1 107 | # 取数据 108 | inputs, labels = data 109 | inputs, labels = inputs.to(device), labels.to(device) # 将输入和目标在每一步都送入GPU 110 | # 将梯度置零 111 | optimizer.zero_grad() 112 | # 训练 113 | outputs = net(inputs) 114 | loss = criterion(outputs, labels).to(device) 115 | loss.backward() # 反向传播 116 | writer.add_scalar('loss', loss.item(), iter) 117 | optimizer.step() # 优化 118 | # 统计数据 119 | running_loss += loss.item() 120 | if i % numPrint == 999: # 每 batchsize * numPrint 张图片,打印一次 121 | print('epoch: %d\t batch: %d\t loss: %.6f' % (epoch + 1, i + 1, running_loss / (batchSize*numPrint))) 122 | running_loss = 0.0 123 | writer.add_scalar('accuracy', Accuracy(), num + 1) 124 | num = num + 1 125 | # 保存模型 126 | torch.save(net, './model.pkl') 127 | 128 | if __name__ == '__main__': 129 | # 如果模型存在,加载模型 130 | if os.path.exists(modelPath): 131 | print('model exits') 132 | net = torch.load(modelPath) 133 | print('model loaded') 134 | else: 135 | print('model not exits') 136 | print('Training Started') 137 | train() 138 | writer.close() 139 | print('Training Finished') 140 | -------------------------------------------------------------------------------- /DeepLearning-Lab/lab2/README.md: -------------------------------------------------------------------------------- 1 | # 实验内容 2 | 3 | * 基于 PyTorch 实现 AlexNet 4 | * 在 Cifar-10 数据集上进行验证 5 | * 使用tensorboard进行训练数据可视化(Loss 曲线) 6 | * 如有条件,尝试不同参数的影响,尝试其他网络结构 7 | * 请勿使用torchvision.models.AlexNet 8 | 9 | # 实验环境 10 | 11 | * Windows10 12 | * Python 3.3 13 | * Pytorch 1.1 14 | * cuda 9.0 15 | 16 | # 实验介绍 17 | 18 | 这是深度学习课程的第二个实验,实验的主要内容是卷积神经网络,要求实现 AlexNet。但是 AlexNet 的输入不符合 Cifar-10 数据集,因此这里将参数更改了一下,但网络结构没有变,还是五层卷积,三层全连接。 19 | 20 | 虽然对于 32 X 32 这么小的图片,用 AlexNet 有点大材小用的感觉,但实验要求,而且对于初学者来说,AlexNet 还是很经典的,能学到不少东西,直接干就好了。 21 | 22 | # 实验过程 23 | 24 | ### 卷积神经网的尺寸计算 25 | 26 | 在写卷积神经网的代码之前,首先,必须要知道一个知识点,就是每层卷积神经网的输入和输出的关系,即卷积过程的尺寸变化。 27 | 28 | 尺寸的变化公式可以按照如下公式进行计算: 29 | 30 | **N = (W − F + 2P )/S+1** 31 | 32 | 其中 33 | 34 | > * W 表示输入图片尺寸(W×W) 35 | > * F 表示卷积核的大小(F×F) 36 | > * S 表示步长 37 | > * P 表示填充的像素数(Padding) 38 | 39 | 最后输出的图像大小是 N X N 40 | 41 | 池化层的计算大致一样,也是利用滤波器的大小 F×F 和 步长的大小 S 进行计算;通常是不加 Padding 的,如果有 Padding, 则完全按照卷积的公式计算即可。 42 | 43 | #### 1. 加载数据集 44 | 45 | 实验中使用的是 CIFAR-10 数据集,CIFAR-10 数据集由 10 个类的 60000 个 32x32 彩色图像组成,每个类有6000个图像。有50000个训练图像和10000个测试图像。 46 | 47 | #### 2. 定义网络结构 48 | 49 | AlexNet 的网络结构是 5 层卷积,3 层全连接,由于输入的图片大小是 32 x 32 的,因此不能完全按照 AlexNet 设计。 50 | 51 | 网络结构见代码,每层的输出大小已经在注释中进行标注 52 | 53 | #### 3. 训练 54 | 55 | 训练过程中,计算损失函数和准确率,最后,在所有 epoch 都跑完后,将模型保存起来。 56 | 57 | #### 4. 测试 58 | 59 | 测试函数在第一个实验中已经写过了,这里没改什么东西 60 | 61 | 62 | # 实验结果 63 | 64 | 3 个 epoch 的准确率大概在 42% 左右,训练后的 loss 和准确率曲线如下: 65 | 66 | Loss 曲线: 67 | 68 | ![Loss 曲线](https://img-blog.csdnimg.cn/2019062011312387.png) 69 | 70 | 准确率曲线: 71 | ![准确率曲线](https://img-blog.csdnimg.cn/2019062011301671.png) 72 | 73 | 最后保存的模型是 20 个 epoch 后的,准确率大概在 66% 左右。 74 | 75 | # 运行 76 | 77 | 运行代码命令: 78 | 79 | > python .\AlexNet.py 80 | 81 | 可视化命令: 82 | 83 | > tensorboard --logdir=./Result 84 | 85 | -------------------------------------------------------------------------------- /DeepLearning-Lab/lab2/Result/events.out.tfevents.1561000326.DESKTOP-T3AN8OM: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab2/Result/events.out.tfevents.1561000326.DESKTOP-T3AN8OM -------------------------------------------------------------------------------- /DeepLearning-Lab/lab2/model.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab2/model.pkl -------------------------------------------------------------------------------- /DeepLearning-Lab/lab2/实验2 - 卷积神经网络实现.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab2/实验2 - 卷积神经网络实现.pptx -------------------------------------------------------------------------------- /DeepLearning-Lab/lab3/README.md: -------------------------------------------------------------------------------- 1 | # 实验内容 2 | 3 | * 基于PyTorch实现 VGG/ResNet 结构,并在 Cifar-10 数据集上进行验证 4 | 1. VGG要求实现VGG-11(Conv部分按论文实现,Classifier直接一层全连接即可) 5 | 2. ResNet要求实现ResNet-18。均要求5个epoch以内达到50%的测试集精度。 6 | 7 | * 基于 VGG 进行训练方式对比(LR 和优化器的对比) 8 | 1. LR对比三组及以上【此时选用任一种优化器】 9 | 2. 优化器对比 SGD 与 Adam【选用LR对比时的最佳LR】 10 | 11 | * 基于 CUDA 实现,并设置 argparse 参数 12 | 可设定是否使用GPU,通过 argparse 包实现,默认参数设定为GPU 13 | 14 | * 自定义实现 dataset 15 | 基于opencv或Pillow 16 | 17 | # 代码运行 18 | 19 | 1. 执行命令: 20 | 21 | > python .\main.py 22 | 23 | 默认使用 gpu 运行,如果想用 CPU 运行,执行命令 `python .\main.py --cuda CPU` 24 | 25 | 2. 首先选择是否执行 VGG-11; 然后再选择是否执行 ResNet 26 | 27 | 3. 如果执行 VGG-11, 需要选择优化器(三种优化器选择:Adam、 SGD、 RMSprop) 28 | 29 | # 实验结果 30 | 31 | ## 执行命令查看实验的loss曲线和准确率曲线 32 | 33 | 1. 查看 VGG-11 实验结果 34 | 35 | > tensorboard --logdir=./logs_vgg 36 | 37 | 准确率曲线 38 | 39 | ![VGG测试准确率曲线](https://img-blog.csdnimg.cn/20190717225218683.png) 40 | 41 | Loss 曲线 42 | 43 | ![VGG测试Loss曲线](https://img-blog.csdnimg.cn/2019071722553918.png) 44 | 45 | 2. 查看 ResNet 实验结果 46 | 47 | > tensorboard --logdir=./logs_resNet 48 | 49 | 准确率曲线 50 | 51 | ![ResNet测试准确率曲线](https://img-blog.csdnimg.cn/20190717225827668.png) 52 | 53 | Loss 曲线 54 | 55 | ![ResNet测试Loss曲线](https://img-blog.csdnimg.cn/20190717230052289.png) 56 | 57 | # 备注 58 | 59 | 1. 代码运行的比较慢,还是用 gpu 速度快一些 60 | 2. 实验是和队友组队完成的,VGG-11 是队友负责的;ResNet 网络是我负责的(已经询问过队友公开代码的问题了,版权意识还是要有的,哈哈) 61 | 3. 实验很多内容都是参考网上的前辈们的,在此感谢各位前辈们的分享。本打算也来写点博客做下贡献,奈何学艺不精,怕误人子弟 -------------------------------------------------------------------------------- /DeepLearning-Lab/lab3/dataset.py: -------------------------------------------------------------------------------- 1 | import torch.utils.data 2 | import pickle 3 | import numpy as np 4 | from PIL import Image 5 | 6 | 7 | # 需要继承data.Dataset 8 | class Cifar10Dataset(torch.utils.data.Dataset): 9 | def __init__(self, root, train=True, transform=None, target_transform=None): 10 | """ 11 | 1. 初始化数据路径 12 | 2. 读取数据 13 | 3. 整理数据 14 | """ 15 | self.root = root + "\\cifar-10-batches-py\\" # 目录 16 | self.fileName = "" 17 | self.data = [] # 存储图片数据 18 | self.targets = [] # 存储图片标签 19 | # 存储 transform 20 | self.transform = transform 21 | self.target_transform = target_transform 22 | 23 | # 读取数据 24 | if train : # 训练集 25 | self.fileName = "data_batch_" 26 | for i in range(1, 6) : 27 | file = self.root + self.fileName + str(i) # 构建文件路径 28 | self.save(file) 29 | else : # 测试集 30 | self.fileName = "test_batch" # test 文件路径 31 | file = self.root + self.fileName 32 | self.save(file) 33 | # 整理读取的数据 34 | self.data = np.vstack(self.data) # 合并二维数组,并转成 numpy.ndarray 类型,方便 reshape 35 | self.data = self.data.reshape(-1, 3, 32, 32) 36 | # print(self.data.shape) # (50000, 3, 32, 32) 37 | self.data = self.data.transpose((0, 2, 3, 1)) # 转置 转成 HWC 格式 38 | # print(self.data.shape) # (50000, 32, 32, 3) 39 | 40 | def __getitem__(self, index): 41 | """ 42 | 1. 按照下标 (index) 读数据 43 | 2. 转换图片格式. 44 | 3. 返回图片数据和标签信息 45 | """ 46 | img, target = self.data[index], self.targets[index] 47 | # 将图片信息转成 PIL.Image.Image 类型 48 | img = Image.fromarray(img) 49 | # 对 PIL.Image 进行变换 50 | if self.transform is not None: 51 | img = self.transform(img) 52 | # 对标签格式进行转换 53 | if self.target_transform is not None: 54 | target = self.target_transform(target) 55 | return img, target 56 | 57 | def __len__(self): 58 | # 返回数据长度 59 | return len(self.data) 60 | 61 | def unpickle(self, file): 62 | with open(file, 'rb') as fo: 63 | dict = pickle.load(fo, encoding='bytes') 64 | return dict 65 | 66 | def save(self, file): 67 | dict = self.unpickle(file) # 利用官网提供的方法读取数据,存储在字典中 68 | self.data.append(dict[b'data']) # 将数据集中的 data 存储起来 69 | self.targets.extend(dict[b'labels']) # list 类型 70 | -------------------------------------------------------------------------------- /DeepLearning-Lab/lab3/logs_resNet/events.out.tfevents.1558866099.LAPTOP-BFCS88S3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab3/logs_resNet/events.out.tfevents.1558866099.LAPTOP-BFCS88S3 -------------------------------------------------------------------------------- /DeepLearning-Lab/lab3/logs_vgg/events.out.tfevents.1559188515.LAPTOP-BFCS88S3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab3/logs_vgg/events.out.tfevents.1559188515.LAPTOP-BFCS88S3 -------------------------------------------------------------------------------- /DeepLearning-Lab/lab3/main.py: -------------------------------------------------------------------------------- 1 | """ 2 | 系统环境:Windows10 3 | python版本:3.6 4 | pytorch版本:torch1.1.0 5 | cuda版本:9.1 6 | cudnn版本:7.1 7 | tensorflow版本:1.8.0 8 | 程序运行:在命令行中输入python main.py,利用argparse设置一个cuda参数,默认值'GPU',还可以通过python main.py --cuda 'CPU'选择CPU运行, 9 | 然后先选择是否执行vgg11网络,执行vgg11时可以选择优化方法;然后继续选择是否执行resNet 10 | 其中vgg中的BN层被注释掉 11 | 可视化图像:将测试集准确率和训练集训练时的loss图像进行可视化,分别为test_accuracy和train_loss曲线,vgg的存在logs_vggw文件夹下,resnet的存在logs_resNet文件夹下 12 | """ 13 | 14 | import torch.utils.data 15 | import argparse 16 | 17 | 18 | if __name__ == '__main__': 19 | parser = argparse.ArgumentParser() 20 | parser.add_argument('--cuda', default='GPU', type=str, choices=['GPU', 'CPU'], help='选择使用cpu/gpu') 21 | args = parser.parse_args() 22 | # CPU或GPU运行 23 | if args.cuda == 'GPU': 24 | device = torch.device('cuda') 25 | elif args.cuda == 'CPU': 26 | device = torch.device('cpu') 27 | # 选择是否执行vgg11 28 | choicess = input('请选择是(1),否(0)运行vgg-11\nYour choice:') 29 | if choicess == '1': 30 | import vgg 31 | vgg.run(device) 32 | # 选择是否执行resNet18 33 | choicess_1 = input('请选择是(1),否(0)运行ResNet-18\nYour choice:') 34 | if choicess_1 == '1': 35 | import resNet 36 | resNet.run(device) 37 | -------------------------------------------------------------------------------- /DeepLearning-Lab/lab3/resNet.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn.functional as F 3 | import torch.nn as nn 4 | import torch.optim as optim 5 | import torchvision.transforms as transforms 6 | from dataset import Cifar10Dataset 7 | from tensorboardX import SummaryWriter 8 | import torchvision 9 | 10 | 11 | class ResidualBlock(nn.Module): 12 | """ 13 | 子 module: Residual Block ---- ResNet 中一个跨层直连的单元 14 | """ 15 | def __init__(self, inchannel, outchannel, stride=1): 16 | super(ResidualBlock, self).__init__() 17 | self.left = nn.Sequential( 18 | nn.Conv2d(inchannel, outchannel, kernel_size=3, stride=stride, padding=1, bias=False), 19 | nn.BatchNorm2d(outchannel), 20 | nn.ReLU(inplace=True), 21 | nn.Conv2d(outchannel, outchannel, kernel_size=3, stride=1, padding=1, bias=False), 22 | nn.BatchNorm2d(outchannel) 23 | ) 24 | self.shortcut = nn.Sequential() 25 | # 如果输入和输出的通道不一致,或其步长不为 1,需要将二者转成一致 26 | if stride != 1 or inchannel != outchannel: 27 | self.shortcut = nn.Sequential( 28 | nn.Conv2d(inchannel, outchannel, kernel_size=1, stride=stride, bias=False), 29 | nn.BatchNorm2d(outchannel) 30 | ) 31 | 32 | def forward(self, x): 33 | out = self.left(x) 34 | out += self.shortcut(x) # 输出 + 输入 35 | out = F.relu(out) 36 | return out 37 | 38 | 39 | class ResNet(nn.Module): 40 | """ 41 | 实现主 module: ResNet-18 42 | ResNet 包含多个 layer, 每个 layer 又包含多个 residual block (上面实现的类) 43 | 因此, 用 ResidualBlock 实现 Residual 部分,用 _make_layer 函数实现 layer 44 | """ 45 | def __init__(self, ResidualBlock, num_classes=10): 46 | super(ResNet, self).__init__() 47 | self.inchannel = 64 48 | # 最开始的操作 49 | self.conv1 = nn.Sequential( 50 | nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False), 51 | nn.BatchNorm2d(64), 52 | nn.ReLU(), 53 | ) 54 | # 四个 layer, 对应 2, 3, 4, 5 层, 每层有两个 residual block 55 | self.layer1 = self._make_layer(ResidualBlock, 64, 2, stride=1) 56 | self.layer2 = self._make_layer(ResidualBlock, 128, 2, stride=2) 57 | self.layer3 = self._make_layer(ResidualBlock, 256, 2, stride=2) 58 | self.layer4 = self._make_layer(ResidualBlock, 512, 2, stride=2) 59 | # 最后的全连接,分类时使用 60 | self.fc = nn.Linear(512, num_classes) 61 | 62 | def _make_layer(self, block, channels, num_blocks, stride): 63 | """ 64 | 构建 layer, 每一个 layer 由多个 residual block 组成 65 | 在 ResNet 中,每一个 layer 中只有两个 residual block 66 | """ 67 | layers = [] 68 | for i in range(num_blocks): 69 | if i == 0: # 第一个是输入的 stride 70 | layers.append(block(self.inchannel, channels, stride)) 71 | else: # 后面的所有 stride,都置为 1 72 | layers.append(block(channels, channels, 1)) 73 | self.inchannel = channels 74 | return nn.Sequential(*layers) # 时序容器。Modules 会以他们传入的顺序被添加到容器中。 75 | 76 | def forward(self, x): 77 | # 最开始的处理 78 | out = self.conv1(x) 79 | # 四层 layer 80 | out = self.layer1(out) 81 | out = self.layer2(out) 82 | out = self.layer3(out) 83 | out = self.layer4(out) 84 | # 全连接 输出分类信息 85 | out = F.avg_pool2d(out, 4) 86 | out = out.view(out.size(0), -1) 87 | out = self.fc(out) 88 | return out 89 | 90 | 91 | def Accuracy(testloader, net, device): 92 | # 使用测试数据测试网络 93 | correct = 0 94 | total = 0 95 | with torch.no_grad(): 96 | for data in testloader: 97 | images, labels = data 98 | # 将输入和目标在每一步都送入GPU 99 | images, labels = images.to(device), labels.to(device) 100 | outputs = net(images) 101 | _, predicted = torch.max(outputs.data, 1) 102 | total += labels.size(0) 103 | correct += (predicted == labels).sum().item() 104 | print('Accuracy of the network on the 10000 test images: %d %%' % ( 105 | 100 * correct / total)) 106 | return 100.0 * correct / total 107 | 108 | 109 | def run(device): 110 | # 超参数设置 111 | # 批处理尺寸(batch_size) 112 | BATCH_SIZE = 128 113 | # 学习率 114 | LR = 0.1 115 | # 加载数据集 116 | # 标准化为范围在[-1, 1]之间的张量 117 | transform = transforms.Compose([transforms.ToTensor(), 118 | transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) 119 | # 数据集 120 | # # 利用自定义 Dataset 121 | # trainset = Cifar10Dataset(root='./Cifar-10', train=True, transform=transform) # 训练数据集 122 | # testset = Cifar10Dataset(root='./Cifar-10', train=False, transform=transform) 123 | # 利用库函数进行数据集加载 124 | trainset = torchvision.datasets.CIFAR10(root='./Cifar-10', train=True, download=True, transform=transform) # 训练数据集 125 | testset = torchvision.datasets.CIFAR10(root='./Cifar-10', train=False, download=True, transform=transform) 126 | # 生成一个个batch进行批训练,组成batch的时候顺序打乱取 127 | trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True) 128 | testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False) 129 | # Cifar-10的标签 130 | # 建立神经网络 131 | net = ResNet(ResidualBlock).to(device) 132 | writer = SummaryWriter('./logs_resNet') 133 | # 定义损失函数 134 | criterion = nn.CrossEntropyLoss() # 损失函数为交叉熵,多用于多分类问题 135 | # 优化方式为mini-batch momentum-SGD,并采用L2正则化(权重衰减) 136 | optimizer = optim.SGD(net.parameters(), lr=LR, momentum=0.9, weight_decay=5e-4) 137 | # 训练网络 138 | # loop over the dataset multiple times 139 | for epoch in range(5): 140 | running_loss = 0.0 141 | for i, data in enumerate(trainloader, 0): 142 | # 取数据 143 | inputs, labels = data 144 | inputs, labels = inputs.to(device), labels.to(device) # 将输入和目标在每一步都送入GPU 145 | # 将梯度置零 146 | optimizer.zero_grad() 147 | # 训练 148 | outputs = net(inputs) 149 | loss = criterion(outputs, labels).to(device) 150 | loss.backward() # 反向传播 151 | optimizer.step() # 优化 152 | # 统计数据 153 | running_loss += loss.item() 154 | if i % 20 == 19: # 每 2000 张图片,打印一次 155 | print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 2000)) 156 | writer.add_scalar('train_loss', running_loss / 2000, epoch * 380 + i + 1) 157 | running_loss = 0.0 158 | acc = Accuracy(testloader, net, device) 159 | writer.add_scalar('test_accuracy', acc, epoch * 380 + i + 1) 160 | 161 | -------------------------------------------------------------------------------- /DeepLearning-Lab/lab3/vgg.py: -------------------------------------------------------------------------------- 1 | import torch.optim as optim 2 | import torch.utils.data 3 | import torch.backends.cudnn as cudnn 4 | from torchvision import transforms as transforms 5 | import torch.nn as nn 6 | import numpy as np 7 | from tensorboardX import SummaryWriter 8 | from dataset import Cifar10Dataset 9 | import torchvision 10 | 11 | class VGG(nn.Module): 12 | 13 | def __init__(self, num_classes=10): 14 | super(VGG, self).__init__() 15 | self.features = nn.Sequential( 16 | # 第一层 17 | # 取卷积核为3*3,补边为1,输入通道为3,输出通道为64 18 | nn.Conv2d(3, 64, kernel_size=3, padding=1), 19 | # 批标准化 20 | # nn.BatchNorm2d(64, affine=True), 21 | # 激活函数 22 | nn.ReLU(inplace=True), 23 | # 池化,核为2*2,步长为2 24 | nn.MaxPool2d(kernel_size=2, stride=2), 25 | 26 | # 第二层 27 | # 取卷积核为3*3,补边为1,输入通道为64,输出通道为128 28 | nn.Conv2d(64, 128, kernel_size=3, padding=1), 29 | # nn.BatchNorm2d(128, affine=True), 30 | nn.ReLU(inplace=True), 31 | nn.MaxPool2d(kernel_size=2, stride=2), 32 | 33 | # 第三层 34 | # 取卷积核为3*3,补边为1,输入通道为128,输出通道为256 35 | nn.Conv2d(128, 256, kernel_size=3, padding=1), 36 | # nn.BatchNorm2d(256, affine=True), 37 | nn.ReLU(inplace=True), 38 | # 取卷积核为3*3,补边为1,输入通道为256,输出通道为256 39 | nn.Conv2d(256, 256, kernel_size=3, padding=1), 40 | # nn.BatchNorm2d(256, affine=True), 41 | nn.ReLU(inplace=True), 42 | nn.MaxPool2d(kernel_size=2, stride=2), 43 | 44 | # 第四层 45 | # 取卷积核为3*3,补边为1,输入通道为256,输出通道为512 46 | nn.Conv2d(256, 512, kernel_size=3, padding=1), 47 | # nn.BatchNorm2d(512, affine=True), 48 | nn.ReLU(inplace=True), 49 | # 取卷积核为3*3,补边为1,输入通道为512,输出通道为512 50 | nn.Conv2d(512, 512, kernel_size=3, padding=1), 51 | # nn.BatchNorm2d(512, affine=True), 52 | nn.ReLU(inplace=True), 53 | nn.MaxPool2d(kernel_size=2, stride=2), 54 | 55 | # 第五层 56 | # 取卷积核为3*3,补边为1,输入通道为512,输出通道为512 57 | nn.Conv2d(512, 512, kernel_size=3, padding=1), 58 | # nn.BatchNorm2d(512, affine=True), 59 | nn.ReLU(inplace=True), 60 | # 取卷积核为3*3,补边为1,输入通道为512,输出通道为512 61 | nn.Conv2d(512, 512, kernel_size=3, padding=1), 62 | # nn.BatchNorm2d(512, affine=True), 63 | nn.ReLU(inplace=True), 64 | nn.MaxPool2d(kernel_size=2, stride=2), 65 | ) 66 | self.classifier = nn.Sequential( 67 | # 一层全连接层,输入512层,输出10(10类) 68 | nn.Linear(512, num_classes), 69 | ) 70 | 71 | def forward(self, x): 72 | x = self.features(x) 73 | x = x.view(x.size(0), -1) 74 | x = self.classifier(x) 75 | return x 76 | 77 | 78 | def train(epoch, model, writer, train_loader, optimizer, loss_func, device): 79 | print("train:") 80 | # loss损失 81 | train_loss = 0 82 | # 准确个数 83 | train_correct = 0 84 | total = 0 85 | i = 0 86 | for batch_num, (data, target) in enumerate(train_loader): 87 | # 训练数据和其对应的标签 88 | data, target = data.to(device), target.to(device) 89 | # 清空上次训练的梯度 90 | optimizer.zero_grad() 91 | # 将训练数据填入模型中,进行预测类别,前向传播 92 | output = model(data) 93 | # 计算损失函数 94 | loss = loss_func(output, target) 95 | # 反向传播 96 | loss.backward() 97 | # 根据计算的梯度更新网络的参数 98 | optimizer.step() 99 | train_loss += loss.item() 100 | # 生成每个数据预测的类别,即概率最大的 101 | prediction = torch.max(output, 1) 102 | total += target.size(0) 103 | # 计算准确个数,以用来计算准确率 104 | train_correct += np.sum(prediction[1].cpu().numpy() == target.cpu().numpy()) 105 | # 每100个batch计算一次平均的loss,绘制loss曲线,曲线名为train_loss 106 | if i % 100 == 99: 107 | avg_loss = train_loss / 100 108 | writer.add_scalar('train_loss', avg_loss, epoch * len(train_loader) + i + 1) 109 | train_loss = 0.0 110 | i += 1 111 | # 返回每次训练,针对于训练集的预测准确率 112 | return 100. * train_correct / total 113 | 114 | 115 | def exam_model(epoch, model, writer, test_loader, loss_func, device): 116 | print("test:") 117 | # loss损失 118 | test_loss = 0 119 | # 预测正确的个数 120 | test_correct = 0 121 | total = 0 122 | i = 0 123 | for batch_num, (data, target) in enumerate(test_loader): 124 | # 测试数据和其对应的标签 125 | data, target = data.to(device), target.to(device) 126 | # 将测试数据填入模型中,进行预测类别 127 | output = model(data) 128 | # 计算损失函数 129 | loss = loss_func(output, target) 130 | test_loss += loss.item() 131 | # 生成每个数据预测的类别,即概率最大的 132 | prediction = torch.max(output, 1) 133 | total += target.size(0) 134 | # 计算准确个数,以用来计算准确率 135 | test_correct += np.sum(prediction[1].cpu().numpy() == target.cpu().numpy()) 136 | # 每25个batch计算一次平均的loss,绘制loss曲线,曲线名为test_loss 137 | if i % 25 == 24: 138 | avg_loss = test_loss / 25 139 | # writer.add_scalar('test_loss', avg_loss, epoch * len(test_loader) + i + 1) 140 | test_loss = 0.0 141 | i += 1 142 | # 每次测试将计算的准确率绘制为曲线test_accuracy 143 | writer.add_scalar('test_accuracy', 100. * test_correct / total, epoch + 1) 144 | # 返回每次测试,针对于测试集的预测准确率 145 | return 100. * test_correct / total 146 | 147 | 148 | def choice(model): 149 | print('=====================================') 150 | print('=== 请选择优化方式 ===') 151 | print('=== 1.Adam ===') 152 | print('=== 2.SGD ===') 153 | print('=== 3.RMSprop ===') 154 | print('=====================================') 155 | choices = int(input()) 156 | if choices == 1: 157 | # 选用Adam作为优化器,学习率为0.001 158 | optimizer = optim.Adam(model.parameters(), lr=0.001) 159 | elif choices == 2: 160 | optimizer = optim.SGD(model.parameters(), lr=0.001) 161 | elif choices == 3: 162 | optimizer = optim.RMSprop(model.parameters(), lr=0.001) 163 | return optimizer 164 | 165 | 166 | # 运行函数,进行模型训练和测试集的测试 167 | def run(device): 168 | # 利用tensorboardX中的SummaryWriter类可视化测试集准确率曲线 169 | writer = SummaryWriter('./logs_vgg') 170 | train_transform = transforms.Compose([transforms.RandomHorizontalFlip(), transforms.ToTensor()]) 171 | test_transform = transforms.Compose([transforms.ToTensor()]) 172 | # # 利用自定义 Dataset 173 | # train_set = Cifar10Dataset(root='./Cifar-10', train=True, transform=transform) # 训练数据集 174 | # test_set = Cifar10Dataset(root='./Cifar-10', train=False, transform=transform) 175 | # 利用库函数进行数据集加载 176 | train_set = torchvision.datasets.CIFAR10(root='./Cifar-10', train=True, download=True, transform=train_transform) # 训练数据集 177 | test_set = torchvision.datasets.CIFAR10(root='./Cifar-10', train=False, download=True, transform=test_transform) 178 | # 将数据集整理成batch的形式并转换为可迭代对象,200行 179 | train_loader = torch.utils.data.DataLoader(dataset=train_set, batch_size=200, shuffle=True) 180 | test_loader = torch.utils.data.DataLoader(dataset=test_set, batch_size=200, shuffle=False) 181 | # 调用编写好的vgg11模型,对环境进行配置 182 | cudnn.benchmark = True 183 | model = VGG().to(device) 184 | # 选择合适的优化器 185 | optimizer = choice(model) 186 | # 计算损失函数 187 | loss_func = nn.CrossEntropyLoss().to(device) 188 | # 设置训练次数为5次 189 | epochs = 5 190 | accuracy = 0 191 | # 进行5次训练,每进行一次训练,就用测试集进行一次测试 192 | for epoch in range(1, epochs + 1): 193 | print('\n===> epoch: %d/5' % epoch) 194 | train_result = train(epoch - 1, model, writer, train_loader, optimizer, loss_func, device) 195 | print('*****训练集预测准确率为: %.3f%%' % train_result) 196 | test_result = exam_model(epoch - 1, model, writer, test_loader, loss_func, device) 197 | print('*****测试集预测准确率为: %.3f%%' % test_result) 198 | accuracy = max(accuracy, test_result) 199 | # 保存训练好的模型 200 | if epoch == epochs: 201 | print('*****测试集最高准确率为: %.3f%%' % accuracy) 202 | torch.save(model, 'vgg11.pt') 203 | -------------------------------------------------------------------------------- /DeepLearning-Lab/lab3/实验3 - VGG&ResNet实现与对比.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab3/实验3 - VGG&ResNet实现与对比.pptx -------------------------------------------------------------------------------- /DeepLearning-Lab/lab3/实验报告.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab3/实验报告.pdf -------------------------------------------------------------------------------- /DeepLearning-Lab/lab4/1160300426-李国建-RNN实验报告.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab4/1160300426-李国建-RNN实验报告.pdf -------------------------------------------------------------------------------- /DeepLearning-Lab/lab4/data_processing.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import numpy as np 3 | import codecs 4 | import numpy.random 5 | 6 | # 读取字典 7 | def readDict(fileName): 8 | dic=[] 9 | with open(fileName, 'r', encoding='utf-8') as f: 10 | for line in f.readlines(): 11 | b=line.split(' ') # 将每一行以空格为分隔符转换成列表 12 | key = b[0] 13 | val = [float(i) for i in b[1:]] 14 | dic.append([key, val]) 15 | dic=dict(dic) 16 | return dic 17 | 18 | # 读取训练集和测试集,进行数据预处理 19 | def readData(posFile, negFile, dic): 20 | # 读取 pos 文件 21 | pos_set = [] 22 | with open(posFile, 'r', encoding="windows-1252", errors='ignore') as f: 23 | for line in f.readlines(): 24 | words = line.split(' ') # 将每一行以空格为分隔符转换成列表 25 | # 每个单词 26 | sentence = dealWords(words, dic) 27 | pos_set.append(sentence) 28 | # 读取 neg 文件 29 | neg_set = [] 30 | with open(negFile, 'r', encoding="windows-1252", errors='ignore') as f: 31 | for line in f.readlines(): 32 | words = line.split(' ') # 将每一行以空格为分隔符转换成列表 33 | # 每个单词 34 | sentence = dealWords(words, dic) 35 | neg_set.append(sentence) 36 | train_set = pos_set[:4000] + neg_set[:4000] 37 | target = [1 for i in range(4000)] + [0 for i in range(4000)] 38 | test_set = pos_set[4000:] + neg_set[4000:] 39 | test_target = [1 for i in range(1331)] + [0 for i in range(1331)] 40 | # 转成 numpy 格式返回 41 | return np.array(train_set), np.array(target), np.array(test_set), np.array(test_target) 42 | 43 | # 处理每一行数据 44 | def dealWords(words, dic): 45 | sentence = [] 46 | ite = 0 47 | # 前面补 0 48 | zero_list = [0 for i in range(50)] 49 | for word in words: 50 | if ite >= 50 : 51 | break 52 | if word in dic.keys(): 53 | sentence.append(dic[word]) 54 | # print(word) 55 | # print(len(list(dic[word]))) 56 | ite += 1 57 | for i in range(50-ite): 58 | sentence.insert(0, zero_list) 59 | return sentence 60 | 61 | # 打乱顺序 62 | def change_order(set, target): 63 | # 打乱顺序 64 | permutation = np.random.permutation(target.shape[0]) 65 | # index = [i for i in range(len(set))] 66 | # np.random.shuffle(index) 67 | return set[permutation, :, :], target[permutation] 68 | 69 | def get_data(): 70 | dic = readDict('./glove.6B.50d.txt') # 读取单词字典 71 | train_set, target, test_set, test_target = readData('rt-polarity.pos', 'rt-polarity.neg', dic) 72 | train_set, target, test_set, test_target = torch.Tensor(train_set), torch.Tensor(target), torch.Tensor(test_set), torch.Tensor(test_target) 73 | # 更改顺序 74 | train_set, target = change_order(train_set, target) 75 | test_set, test_target = change_order(test_set, test_target) 76 | return train_set, target, test_set, test_target 77 | 78 | if __name__ == '__main__': 79 | get_data() 80 | 81 | -------------------------------------------------------------------------------- /DeepLearning-Lab/lab4/emotion_analysis.py: -------------------------------------------------------------------------------- 1 | ''' 2 | python 版本: 3.7.3 3 | pytorch 版本: 1.1 4 | 实验平台: Windows10 5 | ''' 6 | import torch 7 | import numpy as np 8 | import data_processing 9 | 10 | # 使用 cuda 11 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 12 | 13 | # 定义神经网络 14 | class AnalyWithGRU(torch.nn.Module): 15 | def __init__(self, hidden_size, out_size, n_layers=1, batch_size=1): 16 | super(AnalyWithGRU, self).__init__() 17 | self.batch_size = batch_size 18 | self.hidden_size = hidden_size 19 | self.n_layers = n_layers 20 | self.out_size = out_size 21 | # 指定 GRU 的各个参数 22 | self.gru = torch.nn.GRU(hidden_size, hidden_size, n_layers, batch_first=True) 23 | # 最后一层:一个线性层,全连接,用于分类 24 | self.out = torch.nn.Linear(hidden_size, out_size) 25 | 26 | def forward(self, word_inputs, hidden): 27 | ''' 28 | batch_size: batch 的大小,这里默认是1,表示一句话 29 | word_inputs: 输入的向量 30 | hidden: 上下文输出 31 | ''' 32 | # resize 输入的数据 33 | inputs = word_inputs.view(self.batch_size, -1, self.hidden_size) 34 | output, hidden = self.gru(inputs, hidden) 35 | output = self.out(output) 36 | # 仅返回最后一个向量,用 RNN 表示 37 | output = output[:,-1,:] 38 | return output, hidden 39 | def init_hidden(self): 40 | # 每次第一个向量没有上下文,在这里返回一个上下文 41 | hidden = torch.autograd.Variable(torch.zeros(self.n_layers, 1, self.hidden_size)) 42 | return hidden 43 | 44 | # 训练 45 | def train(): 46 | ''' 47 | 隐层 50: 表示词向量的宽度 48 | 输出 2 : 隐层用,输出是分类的个数 49 | ''' 50 | # 获取训练集和测试集 51 | train_set, target, test_set, test_target = data_processing.get_data() 52 | net = AnalyWithGRU(50, 2).to(device) # 定义神经网络 53 | criterion = torch.nn.CrossEntropyLoss() # 设置 loss 54 | optimizer = torch.optim.SGD(net.parameters(), lr=0.001, momentum=0.9) # 设置优化器 55 | for epoch in range(3): 56 | for i in range(train_set.size()[0]): 57 | encoder_hidden = net.init_hidden() 58 | input_data = torch.autograd.Variable(train_set[i]) 59 | output_labels = torch.autograd.Variable(torch.LongTensor([target[i]])) 60 | input_data, encoder_hidden = input_data.to(device), encoder_hidden.to(device) 61 | # 训练 62 | encoder_outputs, encoder_hidden = net(input_data, encoder_hidden) 63 | # 优化器 64 | optimizer.zero_grad() 65 | loss = criterion(encoder_outputs, output_labels.to(device)).to(device) 66 | loss.backward() 67 | optimizer.step() 68 | if i % 1000 == 0 or i==train_set.size()[0]-1: 69 | print("epoch: " + str(epoch+1) + "\t" + "loss: " + str(loss.item())) 70 | Accuracy(net, test_set, test_target) 71 | return 72 | 73 | # 在测试集上进行测试,并计算准确率 74 | def Accuracy(net, test_set, test_target): 75 | # 使用测试数据测试网络 76 | correct = 0 77 | total = 0 78 | with torch.no_grad(): 79 | for i in range(test_set.size()[0]): 80 | encoder_hidden = net.init_hidden() 81 | input_data = torch.autograd.Variable(test_set[i]) 82 | labels = torch.autograd.Variable(torch.LongTensor([test_target[i]])) 83 | input_data, labels = input_data.to(device), labels.to(device) # 将输入和目标在每一步都送入GPU 84 | outputs, _ = net(input_data, encoder_hidden.to(device)) 85 | _, predicted = torch.max(outputs.data, 1) 86 | total += labels.size(0) 87 | correct += (predicted == labels).sum().item() 88 | print('Accuracy of the network on the 1000 test dataset: %d %%' % ( 89 | 100 * correct / total)) 90 | return 100.0 * correct / total 91 | 92 | if __name__ == '__main__': 93 | train() 94 | -------------------------------------------------------------------------------- /DeepLearning-Lab/lab4/glove.6B.50d.txt.7z: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab4/glove.6B.50d.txt.7z -------------------------------------------------------------------------------- /DeepLearning-Lab/lab4/sine.py: -------------------------------------------------------------------------------- 1 | ''' 2 | python 版本: 3.7.3 3 | pytorch 版本: 1.1 4 | 实验平台: Windows10 5 | ''' 6 | # -*- coding: utf-8 -*- 7 | import torch 8 | import torch.nn as nn 9 | import torch.optim as optim 10 | import numpy as np 11 | import matplotlib.pyplot as plt 12 | 13 | # cuda 14 | device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") 15 | print(device) 16 | 17 | # 定义神经网络 18 | class SineWithLSTM(nn.Module): 19 | def __init__(self): 20 | super(SineWithLSTM, self).__init__() 21 | # 第一层 lstm,input: 1 维,hidden: 51 维 22 | self.lstm1 = nn.LSTMCell(1, 51) 23 | # 第二层 lstm, input:51维, hidden: 51 维 24 | self.lstm2 = nn.LSTMCell(51, 51) 25 | # 全连接层 26 | self.linear = nn.Linear(51, 1) 27 | 28 | def forward(self, input, future = 0): 29 | outputs = [] 30 | # 初始化两个 LSTM 31 | h_t = torch.zeros(input.size(0), 51, dtype=torch.double) 32 | c_t = torch.zeros(input.size(0), 51, dtype=torch.double) 33 | h_t2 = torch.zeros(input.size(0), 51, dtype=torch.double) 34 | c_t2 = torch.zeros(input.size(0), 51, dtype=torch.double) 35 | # 分成多个 batch 36 | for i, input_t in enumerate(input.chunk(input.size(1), dim=1)): 37 | input_t = input_t.to(device) 38 | h_t, c_t = h_t.to(device), c_t.to(device) 39 | h_t, c_t = self.lstm1(input_t, (h_t, c_t)) 40 | h_t2, c_t2 = h_t2.to(device), c_t2.to(device) 41 | h_t2, c_t2 = self.lstm2(h_t, (h_t2, c_t2)) 42 | output = self.linear(h_t2) 43 | outputs += [output] 44 | # 进行预测,默认 future 为 0,不进行预测 45 | for i in range(future): 46 | output = output.to(device) 47 | h_t, c_t = h_t.to(device), c_t.to(device) 48 | h_t, c_t = self.lstm1(output, (h_t, c_t)) 49 | h_t2, c_t2 = h_t2.to(device), c_t2.to(device) 50 | h_t2, c_t2 = self.lstm2(h_t, (h_t2, c_t2)) 51 | output = self.linear(h_t2) 52 | outputs += [output] 53 | outputs = torch.stack(outputs, 1).squeeze(2) 54 | return outputs 55 | 56 | # 画线 57 | def draw(y, i): 58 | plt.figure(figsize=(30,10)) 59 | plt.title('sine predict') 60 | plt.xlabel('x') 61 | plt.ylabel('y') 62 | plt.xticks() 63 | plt.yticks() 64 | plt.plot(np.arange(999), y[1][:999], linewidth = 2.0) 65 | plt.plot(np.arange(999, np.shape(y[1])[0]), y[1][999:], linestyle=':', linewidth = 2.0) 66 | plt.savefig('predict%d.jpg'%i) 67 | plt.close() 68 | 69 | # 反向传播,利用 LBFGS 优化器进行优化 70 | def closure(): 71 | optimizer.zero_grad() 72 | out = seq(input) 73 | loss = criterion(out, target).to(device) 74 | print('loss:', loss.item()) 75 | loss.backward() 76 | return loss 77 | 78 | # 训练 79 | def train(): 80 | for i in range(5): 81 | print('STEP: ', i) 82 | optimizer.step(closure) 83 | # 开始预测 84 | with torch.no_grad(): 85 | future = 1000 86 | pred = seq(test_input, future=future) 87 | loss = criterion(pred[:, :-future], test_target) 88 | print('test loss:', loss.item()) 89 | y = pred.detach().cpu().numpy() 90 | # 画线 91 | draw(y, i) 92 | 93 | if __name__ == '__main__': 94 | # 加载训练集和测试集 95 | data = torch.load('./traindata.pt') 96 | # 将数据转成 tensor 格式,并放到 gpu 上运行 97 | input = torch.from_numpy(data[3:, :-1]).to(device) 98 | target = torch.from_numpy(data[3:, 1:]).to(device) 99 | test_input = torch.from_numpy(data[:3, :-1]).to(device) 100 | test_target = torch.from_numpy(data[:3, 1:]).to(device) 101 | # 创建神经网络,也放到 gpu 上 102 | seq = SineWithLSTM().to(device) 103 | seq.double() 104 | criterion = nn.MSELoss() 105 | # 使用 LBFGS 优化器 106 | optimizer = optim.LBFGS(seq.parameters(), lr=0.8) 107 | # 训练 108 | train() 109 | 110 | 111 | -------------------------------------------------------------------------------- /DeepLearning-Lab/lab4/sine_wave_generation.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | 4 | np.random.seed(2) 5 | 6 | T = 20 7 | L = 1000 8 | N = 100 9 | 10 | x = np.empty((N, L), 'int64') 11 | x[:] = np.array(range(L)) + np.random.randint(-4 * T, 4 * T, N).reshape(N, 1) 12 | data = np.sin(x / 1.0 / T).astype('float64') 13 | torch.save(data, open('traindata.pt', 'wb')) 14 | -------------------------------------------------------------------------------- /DeepLearning-Lab/lab4/traindata.pt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab4/traindata.pt -------------------------------------------------------------------------------- /DeepLearning-Lab/lab4/实验4 - 循环神经网络.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab4/实验4 - 循环神经网络.pptx -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/1160300426-李国建-GAN实验报告.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/1160300426-李国建-GAN实验报告.pdf -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/draw.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import torch 4 | import imageio 5 | 6 | # 使用 cuda 7 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 8 | 9 | # 画散点图 10 | def draw_scatter(data, color, x_min, x_max, y_min, y_max): 11 | plt.xlim(x_min, x_max) 12 | plt.ylim(y_min, y_max) 13 | plt.title('Scatter Plot') 14 | plt.xlabel("X", fontsize=14) 15 | plt.ylabel("Y", fontsize=14) 16 | plt.scatter(data[:, 0], data[:, 1], c=color, s=10) 17 | 18 | # 画背景 19 | def draw_background(D, x_min, x_max, y_min, y_max): 20 | i = x_min 21 | bg = [] 22 | while i <= x_max - 0.01: 23 | j = y_min 24 | while j <= y_max - 0.01: 25 | bg.append([i, j]) 26 | j += 0.01 27 | bg.append([i, y_max]) 28 | i += 0.01 29 | j = y_min 30 | while j <= y_max - 0.01: 31 | bg.append([i, j]) 32 | j += 0.01 33 | bg.append([i, y_max]) 34 | bg.append([x_max, y_max]) 35 | color = D(torch.Tensor(bg).to(device)) 36 | bg = np.array(bg) 37 | cm = plt.cm.get_cmap('gray') 38 | sc = plt.scatter(bg[:, 0], bg[:, 1], c= np.squeeze(color.cpu().data), cmap=cm) 39 | # 显示颜色等级 40 | cb = plt.colorbar(sc) 41 | return cb 42 | 43 | # 合成动图 44 | def img_show(net): 45 | imgs = [] 46 | for i in range(1, 21): 47 | fileName = "./result/" + net + "/epoch" + str(i*5) + ".png" 48 | imgs.append(imageio.imread(fileName)) 49 | imageio.mimsave("./result/" + net + "/final.gif", imgs, fps=1) 50 | 51 | if __name__ == '__main__': 52 | img_show("gan") 53 | img_show("wgan") 54 | img_show("wgan_gp") 55 | 56 | -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/gan.py: -------------------------------------------------------------------------------- 1 | from scipy.io import loadmat 2 | import matplotlib.pyplot as plt 3 | import numpy as np 4 | import torch 5 | import torch.nn as nn 6 | import draw 7 | 8 | # 常量定义 9 | BATCH_SIZE = 150 # batch 的大小 10 | EPOCH_NUM = 2000 # epoch 的数量 11 | TRAIN_SET_SIZE = 6000 # 训练集的大小 12 | INPUT_SIZE = 6 # 生成器输入的噪声维度 13 | LR = 0.0003 # 两个网络的学习率 14 | 15 | # 加载数据集 16 | m = loadmat("./points.mat") 17 | data = m['xx'] 18 | # 打乱顺序 19 | np.random.shuffle(data) 20 | # 拆分成训练集和测试集 21 | train_set = data[:TRAIN_SET_SIZE] 22 | test_set = data[TRAIN_SET_SIZE:] 23 | 24 | # 使用 cuda 25 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 26 | 27 | # 获取坐标点 28 | def get_min_data(): 29 | x_min = np.min(test_set[:, 0]) 30 | x_max = np.max(test_set[:, 0]) 31 | y_min = np.min(test_set[:, 1]) 32 | y_max = np.max(test_set[:, 1]) 33 | return x_min, x_max, y_min, y_max 34 | 35 | # 生成网络 36 | class generator(nn.Module): 37 | def __init__(self): 38 | super(generator, self).__init__() 39 | self.gen = nn.Sequential( 40 | nn.Linear(INPUT_SIZE, 256), 41 | nn.ReLU(True), 42 | nn.Linear(256, 512), 43 | nn.ReLU(True), 44 | nn.Linear(512, 2) 45 | # nn.Tanh() # 加上该层会导致分布被截断 46 | ) 47 | 48 | def forward(self, x): 49 | x = self.gen(x) 50 | return x 51 | 52 | 53 | # 判别器 54 | class discriminator(nn.Module): 55 | def __init__(self): 56 | super(discriminator, self).__init__() 57 | # 两层线性全连接 58 | self.dis = nn.Sequential( 59 | nn.Linear(2, 256), 60 | nn.LeakyReLU(0.2), 61 | nn.Linear(256, 256), 62 | nn.LeakyReLU(0.2), 63 | nn.Linear(256, 1), 64 | nn.Sigmoid() 65 | ) 66 | 67 | def forward(self, x): 68 | x = self.dis(x) 69 | return x 70 | 71 | # 定义网络 72 | D = discriminator().to(device) 73 | G = generator().to(device) 74 | # Binary cross entropy loss and optimizer 75 | d_optimizer = torch.optim.SGD(D.parameters(), lr=LR) 76 | g_optimizer = torch.optim.SGD(G.parameters(), lr=LR) 77 | 78 | # 训练 79 | def train(): 80 | for epoch in range(EPOCH_NUM): 81 | for i in range(int(TRAIN_SET_SIZE / BATCH_SIZE)): 82 | label = torch.from_numpy(train_set[i*BATCH_SIZE: (i+1)*BATCH_SIZE]).float().to(device) 83 | G_input = torch.randn(BATCH_SIZE, INPUT_SIZE).to(device) 84 | G_out = G(G_input).to(device) 85 | # 计算判别器判别的概率 86 | prob_label = D(label) 87 | prob_gen = D(G_out) 88 | # 计算生成器和判别器的 Loss 89 | D_loss = - torch.mean(torch.log(prob_label) + torch.log(1. - prob_gen)).to(device) 90 | G_loss = torch.mean(torch.log(1. - prob_gen)).to(device) 91 | # 更新判别器 92 | d_optimizer.zero_grad() 93 | D_loss.backward(retain_graph=True) 94 | d_optimizer.step() 95 | # 更新生成器 96 | g_optimizer.zero_grad() 97 | G_loss.backward() 98 | g_optimizer.step() 99 | print("epoch: %d \t batch: %d \t\t d_loss: %.8f \t g_loss: %.8f "%(epoch+1, i+1, D_loss, G_loss)) 100 | if (epoch+1) % 5 == 0 : 101 | cb = test_G() 102 | plt.savefig('./result/gan/epoch'+ str(epoch+1)) 103 | cb.remove() 104 | plt.cla() 105 | 106 | # 测试训练好的生成器 107 | def test_G(): 108 | G_input = torch.randn(1000, INPUT_SIZE).to(device) 109 | G_out = G(G_input) 110 | G_data = np.array(G_out.cpu().data) 111 | # 计算坐标点边界 112 | x_min, x_max, y_min, y_max = get_min_data() 113 | x_min = np.min(np.append(G_data[:, 0], x_min)) 114 | x_max = np.max(np.append(G_data[:, 0], x_max)) 115 | y_min = np.min(np.append(G_data[:, 1], y_min)) 116 | y_max = np.max(np.append(G_data[:, 1], y_max)) 117 | # 画背景 118 | cb = draw.draw_background(D, x_min, x_max, y_min, y_max) 119 | # 画出测试集的点分布和生成器输出的点分布 120 | draw.draw_scatter(test_set, 'b', x_min, x_max, y_min, y_max) 121 | draw.draw_scatter(G_data, 'r', x_min, x_max, y_min, y_max) 122 | return cb 123 | 124 | if __name__ == '__main__': 125 | train() 126 | test_G() 127 | 128 | -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/points.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/points.mat -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch10.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch100.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch15.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch20.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch25.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch25.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch30.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch30.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch35.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch35.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch40.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch40.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch45.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch45.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch5.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch50.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch50.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch55.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch55.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch60.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch65.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch65.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch70.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch70.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch75.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch75.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch80.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch80.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch85.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch85.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch90.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch90.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/epoch95.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/epoch95.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/gan/final.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/gan/final.gif -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch10.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch100.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch15.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch20.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch25.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch25.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch30.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch30.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch35.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch35.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch40.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch40.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch45.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch45.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch5.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch50.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch50.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch55.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch55.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch60.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch65.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch65.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch70.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch70.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch75.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch75.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch80.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch80.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch85.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch85.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch90.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch90.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/epoch95.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/epoch95.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan/final.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan/final.gif -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch10.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch100.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch15.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch20.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch25.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch25.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch30.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch30.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch35.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch35.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch40.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch40.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch45.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch45.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch5.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch50.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch50.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch55.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch55.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch60.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch65.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch65.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch70.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch70.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch75.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch75.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch80.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch80.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch85.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch85.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch90.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch90.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/epoch95.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/epoch95.png -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/result/wgan_gp/final.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/result/wgan_gp/final.gif -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/wgan.py: -------------------------------------------------------------------------------- 1 | ''' 2 | WGAN 四点主要的改进: 3 | 1. discriminator 的最后一层取消 sigmoid 4 | 2. discriminator 的 w 取值限制在 [-c,c] 区间内, 确保lipschitz连续 5 | 3. 改为非 log 的 loss 6 | 4. 不使用具有动量的优化方法,比如使用Adam,转而使用诸如RMSProp,SGD等方法 (gan 已经实现,不需要改了) 7 | ''' 8 | from scipy.io import loadmat 9 | import matplotlib.pyplot as plt 10 | import numpy as np 11 | import torch 12 | import torch.nn as nn 13 | import draw 14 | 15 | # 常量定义 16 | BATCH_SIZE = 150 # batch 的大小 17 | EPOCH_NUM = 100 # epoch 的数量 18 | TRAIN_SET_SIZE = 6000 # 训练集的大小 19 | INPUT_SIZE = 6 # 生成器输入的噪声维度 20 | LR = 0.0003 # 两个网络的学习率 21 | # WGAN 的权值限制 22 | CLAMP = 0.1 23 | 24 | # 加载数据集 25 | m = loadmat("./points.mat") 26 | data = m['xx'] 27 | # 打乱顺序 28 | np.random.shuffle(data) 29 | # 拆分成训练集和测试集 30 | train_set = data[:TRAIN_SET_SIZE] 31 | test_set = data[TRAIN_SET_SIZE:] 32 | 33 | # 使用 cuda 34 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 35 | 36 | # 获取坐标点 37 | def get_min_data(): 38 | x_min = np.min(test_set[:, 0]) 39 | x_max = np.max(test_set[:, 0]) 40 | y_min = np.min(test_set[:, 1]) 41 | y_max = np.max(test_set[:, 1]) 42 | return x_min, x_max, y_min, y_max 43 | 44 | # 生成网络 45 | class generator(nn.Module): 46 | def __init__(self): 47 | super(generator, self).__init__() 48 | self.gen = nn.Sequential( 49 | nn.Linear(INPUT_SIZE, 256), 50 | nn.ReLU(True), 51 | nn.Linear(256, 256), 52 | nn.ReLU(True), 53 | nn.Linear(256, 2) 54 | # nn.Tanh() # 加上该层会导致分布被截断 55 | ) 56 | 57 | def forward(self, x): 58 | x = self.gen(x) 59 | return x 60 | 61 | 62 | # 判别器 63 | class discriminator(nn.Module): 64 | def __init__(self): 65 | super(discriminator, self).__init__() 66 | # 两层线性全连接 67 | self.dis = nn.Sequential( 68 | nn.Linear(2, 256), 69 | nn.LeakyReLU(0.2), 70 | nn.Linear(256, 256), 71 | nn.LeakyReLU(0.2), 72 | nn.Linear(256, 1), 73 | # 去掉 sigmoid 74 | # nn.Sigmoid() 75 | ) 76 | 77 | def forward(self, x): 78 | x = self.dis(x) 79 | return x 80 | 81 | # 定义网络 82 | D = discriminator().to(device) 83 | G = generator().to(device) 84 | # Binary cross entropy loss and optimizer 85 | d_optimizer = torch.optim.RMSprop(D.parameters(), lr=LR) 86 | g_optimizer = torch.optim.RMSprop(G.parameters(), lr=LR) 87 | 88 | # 训练 89 | def train(): 90 | for epoch in range(EPOCH_NUM): 91 | for i in range(int(TRAIN_SET_SIZE / BATCH_SIZE)): 92 | label = torch.from_numpy(train_set[i*BATCH_SIZE: (i+1)*BATCH_SIZE]).float().to(device) 93 | G_input = torch.randn(BATCH_SIZE, INPUT_SIZE).to(device) 94 | G_out = G(G_input).to(device) 95 | # 计算判别器判别的概率 96 | prob_label = D(label) 97 | prob_gen = D(G_out) 98 | # 计算生成器和判别器的 Loss, 取消 log 99 | D_loss = torch.mean(prob_gen - prob_label).to(device) 100 | G_loss = - torch.mean(prob_gen).to(device) 101 | # 更新判别器 102 | d_optimizer.zero_grad() 103 | D_loss.backward(retain_graph=True) 104 | d_optimizer.step() 105 | 106 | # D 的参数被限制 107 | for p in D.parameters(): 108 | # print(p.data) 109 | p.data.clamp_(-CLAMP, CLAMP) 110 | 111 | # 更新生成器 112 | g_optimizer.zero_grad() 113 | G_loss.backward() 114 | g_optimizer.step() 115 | print("epoch: %d \t batch: %d \t\t d_loss: %.8f \t g_loss: %.8f "%(epoch+1, i+1, D_loss, G_loss)) 116 | if (epoch+1) % 5 == 0 : 117 | cb = test_G() 118 | plt.savefig('./result/wgan/epoch'+ str(epoch+1)) 119 | cb.remove() 120 | plt.cla() 121 | 122 | # 测试训练好的生成器 123 | def test_G(): 124 | G_input = torch.randn(1000, INPUT_SIZE).to(device) 125 | G_out = G(G_input) 126 | G_data = np.array(G_out.cpu().data) 127 | # 计算坐标点边界 128 | x_min, x_max, y_min, y_max = get_min_data() 129 | x_min = np.min(np.append(G_data[:, 0], x_min)) 130 | x_max = np.max(np.append(G_data[:, 0], x_max)) 131 | y_min = np.min(np.append(G_data[:, 1], y_min)) 132 | y_max = np.max(np.append(G_data[:, 1], y_max)) 133 | # 画背景 134 | cb = draw.draw_background(D, x_min, x_max, y_min, y_max) 135 | # 画出测试集的点分布和生成器输出的点分布 136 | draw.draw_scatter(test_set, 'b', x_min, x_max, y_min, y_max) 137 | draw.draw_scatter(G_data, 'r', x_min, x_max, y_min, y_max) 138 | return cb 139 | 140 | if __name__ == '__main__': 141 | train() 142 | test_G() 143 | -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/wgan_gp.py: -------------------------------------------------------------------------------- 1 | ''' 2 | WGAN-GP 主要的改进: 3 | 相比于 WGAN,增加了梯度惩罚项 4 | ''' 5 | from scipy.io import loadmat 6 | import matplotlib.pyplot as plt 7 | import numpy as np 8 | import torch 9 | import torch.nn as nn 10 | import torch.autograd as autograd 11 | import draw 12 | 13 | # 常量定义 14 | BATCH_SIZE = 150 # batch 的大小 15 | EPOCH_NUM = 100 # epoch 的数量 16 | TRAIN_SET_SIZE = 6000 # 训练集的大小 17 | INPUT_SIZE = 6 # 生成器输入的噪声维度 18 | LR = 0.0003 # 两个网络的学习率 19 | # WGAN 的权值限制 20 | CLAMP = 0.1 21 | 22 | # 加载数据集 23 | m = loadmat("./points.mat") 24 | data = m['xx'] 25 | # 打乱顺序 26 | np.random.shuffle(data) 27 | # 拆分成训练集和测试集 28 | train_set = data[:TRAIN_SET_SIZE] 29 | test_set = data[TRAIN_SET_SIZE:] 30 | 31 | # 使用 cuda 32 | cuda = torch.cuda.is_available() 33 | device = torch.device("cuda" if cuda else "cpu") 34 | 35 | # 获取坐标点 36 | def get_min_data(): 37 | x_min = np.min(test_set[:, 0]) 38 | x_max = np.max(test_set[:, 0]) 39 | y_min = np.min(test_set[:, 1]) 40 | y_max = np.max(test_set[:, 1]) 41 | return x_min, x_max, y_min, y_max 42 | 43 | # 生成网络 44 | class generator(nn.Module): 45 | def __init__(self): 46 | super(generator, self).__init__() 47 | self.gen = nn.Sequential( 48 | nn.Linear(INPUT_SIZE, 256), 49 | nn.ReLU(True), 50 | nn.Linear(256, 256), 51 | nn.ReLU(True), 52 | nn.Linear(256, 2) 53 | # nn.Tanh() # 加上该层会导致分布被截断 54 | ) 55 | 56 | def forward(self, x): 57 | x = self.gen(x) 58 | return x 59 | 60 | 61 | # 判别器 62 | class discriminator(nn.Module): 63 | def __init__(self): 64 | super(discriminator, self).__init__() 65 | # 两层线性全连接 66 | self.dis = nn.Sequential( 67 | nn.Linear(2, 256), 68 | nn.LeakyReLU(0.2), 69 | nn.Linear(256, 256), 70 | nn.LeakyReLU(0.2), 71 | nn.Linear(256, 1), 72 | # 去掉 sigmoid 73 | # nn.Sigmoid() 74 | ) 75 | 76 | def forward(self, x): 77 | x = self.dis(x) 78 | return x 79 | 80 | # 定义网络 81 | D = discriminator().to(device) 82 | G = generator().to(device) 83 | # Binary cross entropy loss and optimizer 84 | d_optimizer = torch.optim.RMSprop(D.parameters(), lr=LR) 85 | g_optimizer = torch.optim.RMSprop(G.parameters(), lr=LR) 86 | 87 | # 训练 88 | def train(): 89 | for epoch in range(EPOCH_NUM): 90 | for i in range(int(TRAIN_SET_SIZE / BATCH_SIZE)): 91 | label = torch.from_numpy(train_set[i*BATCH_SIZE: (i+1)*BATCH_SIZE]).float().to(device) 92 | G_input = torch.randn(BATCH_SIZE, INPUT_SIZE).to(device) 93 | G_out = G(G_input).to(device) 94 | 95 | prob_label = D(label) 96 | prob_gen = D(G_out) 97 | 98 | # 计算判别器判别的概率 99 | d_fake = D(G_out) 100 | d_fake_loss = d_fake.mean() 101 | # computes gradientpenalty 102 | gradient_penalty = compute_gradient_penalty(label, G_out) 103 | # D backward and step 104 | D_loss = (- prob_label.mean() + d_fake_loss + .001 * gradient_penalty).to(device) 105 | 106 | # prob_label = D(label) 107 | # prob_gen = D(G_out) 108 | # 计算生成器和判别器的 Loss, 取消 log 109 | # D_loss = torch.mean(prob_label - prob_gen) 110 | G_loss = - torch.mean(prob_gen).to(device) 111 | # 更新判别器 112 | d_optimizer.zero_grad() 113 | D_loss.backward(retain_graph=True) 114 | d_optimizer.step() 115 | 116 | # D 的参数被限制 117 | for p in D.parameters(): 118 | # print(p.data) 119 | p.data.clamp_(-1*CLAMP, CLAMP) 120 | 121 | # 更新生成器 122 | g_optimizer.zero_grad() 123 | G_loss.backward() 124 | g_optimizer.step() 125 | print("epoch: %d \t batch: %d \t\t d_loss: %.8f \t g_loss: %.8f "%(epoch+1, i+1, D_loss, G_loss)) 126 | if (epoch+1) % 5 == 0 : 127 | cb = test_G() 128 | plt.savefig('./result/wgan_gp/epoch'+ str(epoch+1)) 129 | cb.remove() 130 | plt.cla() 131 | 132 | # 测试训练好的生成器 133 | def test_G(): 134 | G_input = torch.randn(1000, INPUT_SIZE).to(device) 135 | G_out = G(G_input) 136 | G_data = np.array(G_out.cpu().data) 137 | # 计算坐标点边界 138 | x_min, x_max, y_min, y_max = get_min_data() 139 | x_min = np.min(np.append(G_data[:, 0], x_min)) 140 | x_max = np.max(np.append(G_data[:, 0], x_max)) 141 | y_min = np.min(np.append(G_data[:, 1], y_min)) 142 | y_max = np.max(np.append(G_data[:, 1], y_max)) 143 | # 画背景 144 | cb = draw.draw_background(D, x_min, x_max, y_min, y_max) 145 | # 画出测试集的点分布和生成器输出的点分布 146 | draw.draw_scatter(test_set, 'b', x_min, x_max, y_min, y_max) 147 | draw.draw_scatter(G_data, 'r', x_min, x_max, y_min, y_max) 148 | return cb 149 | 150 | # 计算惩罚项 151 | def compute_gradient_penalty(real_samples, fake_samples): 152 | Tensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor 153 | alpha = torch.rand(real_samples.shape).to(device) 154 | interpolates = (alpha * real_samples + ((1 - alpha) * fake_samples)).requires_grad_(True).to(device) 155 | d_interpolates = D(interpolates) 156 | fake = autograd.Variable(Tensor(real_samples.shape[0], 1).fill_(1.0), requires_grad=False) 157 | gradients = autograd.grad( 158 | outputs=d_interpolates, 159 | inputs=interpolates, 160 | grad_outputs=fake, 161 | create_graph=True, 162 | retain_graph=True, 163 | only_inputs=True, 164 | )[0] 165 | gradients = gradients.view(gradients.size(0), -1) 166 | gradient_penalty = ((gradients.norm(2, dim=1) -1)**2).mean() 167 | return gradient_penalty 168 | 169 | if __name__ == '__main__': 170 | train() 171 | test_G() 172 | -------------------------------------------------------------------------------- /DeepLearning-Lab/lab5/实验5 - 生成式对抗网络.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/DeepLearning-Lab/lab5/实验5 - 生成式对抗网络.pdf -------------------------------------------------------------------------------- /Pattern-Recognition-Lab/GMM/featureextraction.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from sklearn import preprocessing 3 | #import python_speech_features as mfcc 4 | from python_speech_features import mfcc 5 | 6 | def calculate_delta(array): 7 | """Calculate and returns the delta of given feature vector matrix""" 8 | 9 | rows,cols = array.shape 10 | deltas = np.zeros((rows,20)) 11 | N = 2 12 | for i in range(rows): 13 | index = [] 14 | j = 1 15 | while j <= N: 16 | if i-j < 0: 17 | first =0 18 | else: 19 | first = i-j 20 | if i+j > rows-1: 21 | second = rows-1 22 | else: 23 | second = i+j 24 | index.append((second,first)) 25 | j+=1 26 | deltas[i] = ( array[index[0][0]]-array[index[0][1]] + (2 * (array[index[1][0]]-array[index[1][1]])) ) / 10 27 | return deltas 28 | 29 | def extract_features(audio,rate): 30 | """extract 20 dim mfcc features from an audio, performs CMS and combines 31 | delta to make it 40 dim feature vector""" 32 | 33 | # mfcc_feature = mfcc.mfcc(audio,rate, 0.025, 0.01,20,nfft = 1200, appendEnergy = True) 34 | mfcc_feature = mfcc(audio, rate, 0.025, 0.01, 20, nfft=1200, appendEnergy=True) 35 | mfcc_feature = preprocessing.scale(mfcc_feature) 36 | delta = calculate_delta(mfcc_feature) 37 | combined = np.hstack((mfcc_feature,delta)) 38 | return combined 39 | -------------------------------------------------------------------------------- /Pattern-Recognition-Lab/GMM/modeltraining.py: -------------------------------------------------------------------------------- 1 | import pickle 2 | import numpy as np 3 | from scipy.io.wavfile import read 4 | from sklearn.mixture import GaussianMixture 5 | from featureextraction import extract_features 6 | #from speakerfeatures import extract_features 7 | import warnings 8 | warnings.filterwarnings("ignore") 9 | 10 | #path to training data 11 | # source = "development_set/" 12 | source = "trainingData/" 13 | 14 | #path where training speakers will be saved 15 | 16 | # dest = "speaker_models/" 17 | # train_file = "development_set_enroll.txt" 18 | 19 | dest = "Speakers_models/" 20 | train_file = "trainingDataPath.txt" 21 | file_paths = open(train_file,'r') 22 | 23 | count = 1 24 | # Extracting features for each speaker (5 files per speakers) 25 | features = np.asarray(()) 26 | print(file_paths) 27 | for path in file_paths: 28 | 29 | path = path.strip() 30 | # print path 31 | print(path) 32 | # read the audio 33 | sr,audio = read(source + path) 34 | # extract 40 dimensional MFCC & delta MFCC features 35 | vector = extract_features(audio,sr) 36 | 37 | if features.size == 0: 38 | features = vector 39 | else: 40 | features = np.vstack((features, vector)) 41 | # when features of 45 files of speaker are concatenated, then do model training 42 | # -> if count == 45: --> edited below 43 | if count == 15: 44 | gmm = GaussianMixture(n_components = 18, max_iter = 300, covariance_type='diag', n_init = 5) 45 | gmm.fit(features) 46 | 47 | # dumping the trained gaussian model 48 | picklefile = path.split("-")[0]+".gmm" 49 | #print picklefile 50 | pickle.dump(gmm,open(dest + picklefile,'wb')) 51 | print('+ modeling completed for speaker:' + str(picklefile) + " with data point = " + str(features.shape)) 52 | features = np.asarray(()) 53 | count = 0 54 | count = count + 1 55 | -------------------------------------------------------------------------------- /Pattern-Recognition-Lab/GMM/test.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pickle 3 | import numpy as np 4 | from scipy.io.wavfile import read 5 | from featureextraction import extract_features 6 | #from speakerfeatures import extract_features 7 | import warnings 8 | warnings.filterwarnings("ignore") 9 | import time 10 | 11 | """ 12 | #path to training data 13 | source = "development_set/" 14 | modelpath = "speaker_models/" 15 | test_file = "development_set_test.txt" 16 | file_paths = open(test_file,'r') 17 | 18 | """ 19 | #path to training data 20 | source = "SampleData/" 21 | 22 | #path where training speakers will be savecopy_regd 23 | modelpath = "Speakers_models/" 24 | 25 | gmm_files = [os.path.join(modelpath,fname) for fname in os.listdir(modelpath) if fname.endswith('.gmm')] 26 | print(gmm_files) 27 | #Load the Gaussian gender Models 28 | models = [pickle.load(open(fname,'rb')) for fname in gmm_files] 29 | speakers = [fname.split("/")[-1].split(".gmm")[0] for fname in gmm_files] 30 | 31 | error = 0 32 | total_sample = 0.0 33 | 34 | test_file = "testSamplePath.txt" 35 | file_paths = open(test_file,'r') 36 | # Read the test directory and get the list of test audio files 37 | for path in file_paths: 38 | total_sample = total_sample + 1.0 39 | path = path.strip() 40 | print("Testing Audio : " + str(path)) 41 | sr,audio = read(source + path) 42 | vector = extract_features(audio,sr) 43 | log_likelihood = np.zeros(len(models)) 44 | for i in range(len(models)): 45 | gmm = models[i] #checking with each model one by one 46 | scores = np.array(gmm.score(vector)) 47 | log_likelihood[i] = scores.sum() 48 | 49 | winner = np.argmax(log_likelihood) 50 | print("\tdetected as - " + speakers[winner]) 51 | checker_name = path.split("_")[0] 52 | if speakers[winner] != checker_name: 53 | error = error + 1 54 | time.sleep(1.0) 55 | 56 | print("erro:\t" + str(error) + "\ttotal:\t" + str(total_sample)) 57 | accuracy = ((total_sample - error) / total_sample) * 100 58 | 59 | print("The Accuracy Percentage for the current testing Performance with MFCC + GMM is : " + str(accuracy) + "%") 60 | print("Hurrey ! Speaker identified. Mission Accomplished Successfully. ") 61 | -------------------------------------------------------------------------------- /Pattern-Recognition-Lab/GMM/testSamplePath.txt: -------------------------------------------------------------------------------- 1 | Dog_test1.wav 2 | Dog_test2.wav 3 | Dog_test3.wav 4 | Dog_test4.wav 5 | Dog_test5.wav 6 | Cat_test1.wav 7 | Cat_test2.wav 8 | Cat_test3.wav 9 | Cat_test4.wav 10 | Cat_test5.wav 11 | Bird_test1.wav 12 | Bird_test2.wav 13 | Bird_test3.wav 14 | Bird_test4.wav 15 | Bird_test5.wav 16 | -------------------------------------------------------------------------------- /Pattern-Recognition-Lab/GMM/trainingDataPath.txt: -------------------------------------------------------------------------------- 1 | Cat-1/AngryCat_1.wav 2 | Cat-1/AngryCat_2.wav 3 | Cat-1/AngryCat_3.wav 4 | Cat-1/AngryCat_4.wav 5 | Cat-1/AngryCat_5.wav 6 | Cat-1/CryingCat_1.wav 7 | Cat-1/CryingCat_2.wav 8 | Cat-1/CryingCat_3.wav 9 | Cat-1/CryingCat_4.wav 10 | Cat-1/CryingCat_5.wav 11 | Cat-1/LivelyCat_1.wav 12 | Cat-1/LivelyCat_2.wav 13 | Cat-1/LivelyCat_3.wav 14 | Cat-1/LivelyCat_4.wav 15 | Cat-1/LivelyCat_5.wav 16 | Dog-1/BadDog_1.wav 17 | Dog-1/BadDog_2.wav 18 | Dog-1/BadDog_3.wav 19 | Dog-1/BadDog_4.wav 20 | Dog-1/BadDog_5.wav 21 | Dog-1/FierceDog_1.wav 22 | Dog-1/FierceDog_2.wav 23 | Dog-1/FierceDog_3.wav 24 | Dog-1/FierceDog_4.wav 25 | Dog-1/FierceDog_5.wav 26 | Dog-1/SmallDog_1.wav 27 | Dog-1/SmallDog_2.wav 28 | Dog-1/SmallDog_3.wav 29 | Dog-1/SmallDog_4.wav 30 | Dog-1/SmallDog_5.wav 31 | Bird-1/Cuckoo_1.wav 32 | Bird-1/Cuckoo_2.wav 33 | Bird-1/Cuckoo_3.wav 34 | Bird-1/Cuckoo_4.wav 35 | Bird-1/Cuckoo_5.wav 36 | Bird-1/Sparrow_1.wav 37 | Bird-1/Sparrow_2.wav 38 | Bird-1/Sparrow_3.wav 39 | Bird-1/Sparrow_4.wav 40 | Bird-1/Sparrow_5.wav 41 | Bird-1/Starling_1.wav 42 | Bird-1/Starling_2.wav 43 | Bird-1/Starling_3.wav 44 | Bird-1/Starling_4.wav 45 | Bird-1/Starling_5.wav 46 | -------------------------------------------------------------------------------- /Pattern-Recognition-Lab/lab1-pca.py: -------------------------------------------------------------------------------- 1 | from os import walk, path 2 | import numpy as np 3 | import mahotas as mh 4 | from sklearn.model_selection import train_test_split 5 | from sklearn.model_selection import cross_val_score 6 | from sklearn.preprocessing import scale 7 | from sklearn.decomposition import PCA 8 | from sklearn.linear_model import LogisticRegression 9 | from sklearn.metrics import classification_report 10 | X = [] 11 | y = [] 12 | 13 | # 忽略 警告 14 | import warnings 15 | warnings.filterwarnings("ignore") 16 | 17 | #把照片导入Numpy数组,然后把它们的像素矩阵转换成向量: 18 | for dir_path, dir_names, file_names in walk('orl_faces/'): 19 | for fn in file_names: 20 | if fn[-3:] == 'pgm': 21 | image_filename = path.join(dir_path, fn) 22 | X.append(scale(mh.imread(image_filename, as_grey=True).reshape(10304).astype('float32'))) 23 | y.append(dir_path) 24 | X = np.array(X) 25 | 26 | #用交叉检验建立训练集和测试集,在训练集上用PCA 27 | X_train, X_test, y_train, y_test = train_test_split(X, y) 28 | pca = PCA(n_components=30) # 80 150 30 20 29 | 30 | # 把所有样本降到150维,然后训练一个逻辑回归分类器。数据集包括40个类;scikit-learn底层会自动用one versus all策略创建二元分类器: 31 | X_train_reduced = pca.fit_transform(X_train) 32 | X_test_reduced = pca.transform(X_test) 33 | print('训练集数据的原始维度是:{}'.format(X_train.shape)) 34 | print('PCA降维后训练集数据是:{}'.format(X_train_reduced.shape)) 35 | classifier = LogisticRegression() 36 | accuracies = cross_val_score(classifier, X_train_reduced, y_train) 37 | 38 | #最后,用交叉验证和测试集评估分类器的性能。分类器的平均综合评价指标(F1 score)是0.88,但是需要花费更多的时间训练,在更多训练实例的应用中可能会更慢。 39 | 40 | print('交叉验证准确率是:{}\n{}'.format(np.mean(accuracies), accuracies)) 41 | classifier.fit(X_train_reduced, y_train) 42 | predictions = classifier.predict(X_test_reduced) 43 | print(classification_report(y_test, predictions)) 44 | -------------------------------------------------------------------------------- /Pattern-Recognition-Lab/lab1-svm.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from sklearn import datasets 3 | from sklearn.preprocessing import scale 4 | from sklearn import svm 5 | from sklearn.model_selection import train_test_split 6 | import matplotlib.pyplot as plt 7 | 8 | digits = datasets.load_digits() 9 | # 数据归一化 10 | data = scale(digits.data) 11 | X_train, X_test, y_train, y_test, images_train, images_test = train_test_split(data, digits.target, digits.images, test_size=0.25, random_state=42) 12 | 13 | print("训练集" + str(X_train.shape)) 14 | print("测试集" +str(X_test.shape)) 15 | 16 | # 创建 SVC 模型 17 | svc_model = svm.SVC(gamma=0.001, C=100, kernel='linear') 18 | # 将训练集应用到 SVC 模型上 19 | svc_model.fit(X_train, y_train) 20 | # 评估模型的预测效果 21 | print(svc_model.score(X_test, y_test)) 22 | # 优化参数 23 | svc_model = svm.SVC(gamma=0.001, C=10, kernel='rbf') 24 | svc_model.fit(X_train, y_train) 25 | print(svc_model.score(X_test, y_test)) 26 | # 使用创建的 SVC 模型对测试集进行预测 27 | predicted = svc_model.predict(X_test) 28 | 29 | X = np.arange(len(y_test)) 30 | # 生成比较列表,如果预测的结果正确,则对应位置为0,错误则为1 31 | comp = [0 if y1 == y2 else 1 for y1, y2 in zip(y_test, predicted)] 32 | 33 | print("测试集数量:" + str(len(y_test))) 34 | print("错误识别数:" + str(sum(comp))) 35 | print("识别准确率:" + str(1 - float(sum(comp)) / len(y_test))) 36 | 37 | # 收集错误识别的样本下标 38 | wrong_index = [] 39 | for i, value in enumerate(comp): 40 | if value: wrong_index.append(i) 41 | 42 | # 输出错误识别的样本图像 43 | plt.figure(figsize=(8, 6)) 44 | for plot_index, image_index in enumerate(wrong_index): 45 | image = images_test[image_index] 46 | plt.subplot(2, 4, plot_index + 1) 47 | plt.axis('off') 48 | plt.imshow(image, cmap=plt.cm.gray_r,interpolation='nearest') 49 | # 图像说明,8->9 表示正确值为8,被错误地识别成了9 50 | info = "{right}->{wrong}".format(right=y_test[image_index], wrong=predicted[image_index]) 51 | plt.title(info, fontsize=16) 52 | plt.show() 53 | -------------------------------------------------------------------------------- /Pattern-Recognition-Lab/实验报告-1160300426-李国建.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/Pattern-Recognition-Lab/实验报告-1160300426-李国建.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 介绍 2 | 3 | 本仓库为哈尔滨工业大学 2019 春模式识别与深度学习的实验+课件 4 | 5 | * Pattern-Recognition-Lab 部分是模式识别的实验,一个实验,只放了代码+报告,没有放数据集 6 | 7 | * DeepLearning-Lab 部分是深度学习的实验,一共 6 个实验,最后一次实验自由发挥,没有放到仓库里 8 | 9 | # Pattern-Recognition-Lab(模式识别实验) 10 | 11 | ### 实验内容 12 | 13 | 1. 从 SVM, PCA, GMM, HMM 四个练习中选三个进行训练,可以调用各种库函数,要求实现完整的功能 14 | 15 | 2. 或是实现一个复杂的系统,要求使用机器学习的知识 16 | 17 | ### 说明 18 | 19 | 我做的是第一个,选择了 SVM, PCA, GMM 三个项目。 20 | 21 | 其中 GMM 项目参考:[自制数据集实现动物种类语音识别](https://github.com/LiGuiye/MFCC-GMM-python2.7) 22 | 23 | # DeepLearning-Lab(深度学习实验) 24 | 25 | 也是第一次写深度学习的代码,一路磕磕绊绊,还好全部完成了。 26 | 27 | 代码还有很多改进的空间,留待以后慢慢探索。 28 | 29 | 这里还是简写一下吧,简单介绍一下,有个大概的认识,具体内容见 DeepLearning-Lab 文件夹 30 | 31 | ## 实验一(MLP) 32 | 33 | * 使用PyTorch实现MLP,并在MNIST数据集上验证 34 | 35 | ## 实验二(AlexNet) 36 | 37 | * 基于 PyTorch 实现 AlexNet 38 | * 在 Cifar-10 数据集上进行验证 39 | 40 | ## 实验三(VGG/ResNet) 41 | 42 | * 基于PyTorch实现 VGG/ResNet 结构,并在 Cifar-10 数据集上进行验证 43 | 44 | * 基于 VGG 进行训练方式对比(LR 和优化器的对比) 45 | 46 | ## 实验四(RNN) 47 | 48 | * 对Sine函数进行预测 49 | 50 | * 基于影评数据进行文本情感预测 51 | 52 | ## 实验五(GAN) 53 | 54 | * 基于PyTorch实现生成对抗网络 55 | 56 | * 对比GAN、WGAN、WGAN-GP(稳定性、性能) 57 | 58 | ## 实验六(自选项目,组队完成,暂时没有放到仓库里) 59 | 60 | * 基于PyTorch完成一个项目 61 | 62 | # 最后 63 | 64 | 初次体验神经网络,很神奇。 65 | 66 | 代码肯定有很多不好的地方,以后慢慢改。 67 | 68 | 建仓库的目的,主要还是为了以后写代码,我觉得课程的实验真的让我学到了很多,在这里保存代码,方便以后借鉴 69 | -------------------------------------------------------------------------------- /lecture/DeepLearning/13. 多层感知器.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/DeepLearning/13. 多层感知器.pdf -------------------------------------------------------------------------------- /lecture/DeepLearning/14. 多层感知器-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/DeepLearning/14. 多层感知器-2.pdf -------------------------------------------------------------------------------- /lecture/DeepLearning/15. 卷积神经网络-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/DeepLearning/15. 卷积神经网络-1.pdf -------------------------------------------------------------------------------- /lecture/DeepLearning/16. 卷积神经网络-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/DeepLearning/16. 卷积神经网络-2.pdf -------------------------------------------------------------------------------- /lecture/DeepLearning/17. 时序神经网络-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/DeepLearning/17. 时序神经网络-1.pdf -------------------------------------------------------------------------------- /lecture/DeepLearning/18. 时序神经网络-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/DeepLearning/18. 时序神经网络-2.pdf -------------------------------------------------------------------------------- /lecture/DeepLearning/19. 生成式对抗网络.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/DeepLearning/19. 生成式对抗网络.pdf -------------------------------------------------------------------------------- /lecture/DeepLearning/20. 图像增强与复原.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/DeepLearning/20. 图像增强与复原.pdf -------------------------------------------------------------------------------- /lecture/DeepLearning/21.目标检测与分割0614v2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/DeepLearning/21.目标检测与分割0614v2.pdf -------------------------------------------------------------------------------- /lecture/PatternRecognition/2019-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/PatternRecognition/2019-1.pdf -------------------------------------------------------------------------------- /lecture/PatternRecognition/2019-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/PatternRecognition/2019-2.pdf -------------------------------------------------------------------------------- /lecture/PatternRecognition/2019-3线性分类器与支持向量机.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/PatternRecognition/2019-3线性分类器与支持向量机.pdf -------------------------------------------------------------------------------- /lecture/PatternRecognition/2019-4讲特征选择与特征提取.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/PatternRecognition/2019-4讲特征选择与特征提取.pdf -------------------------------------------------------------------------------- /lecture/PatternRecognition/2019-5 统计分类器及其学习I.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/PatternRecognition/2019-5 统计分类器及其学习I.pdf -------------------------------------------------------------------------------- /lecture/PatternRecognition/2019-6 统计分类器及其学习II.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/PatternRecognition/2019-6 统计分类器及其学习II.pdf -------------------------------------------------------------------------------- /lecture/PatternRecognition/2019-7 聚类分析.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/PatternRecognition/2019-7 聚类分析.pdf -------------------------------------------------------------------------------- /lecture/PatternRecognition/矩阵分析简介.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/PatternRecognition/矩阵分析简介.pdf -------------------------------------------------------------------------------- /lecture/PatternRecognition/矩阵的微分.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/PatternRecognition/矩阵的微分.pdf -------------------------------------------------------------------------------- /lecture/~$2019-1.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rocketeerli/PatternRecognitionAndDeepLearning-Lab/1c66b85b4435d12136011eb6562df0892cd524a9/lecture/~$2019-1.pptx --------------------------------------------------------------------------------